Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
In visual basic.NET a user has a combo box; this combo box is filled with the names of either files or directories with files in them. When the user selects a item in the combo box I want the program to first determine if the name is a filename or a directory name, and then copy it (and all its contents if item is directory) to a predetermined location that is already hardcoded into the program. I am not sure how to accomplish this any help or examples would be greatly appreciated.
Posted (edited)

start with a class like this (C# because vb is mess) . . .

public class FileSystemItem
{
 System.IO.FileSystemInfo _obj;
 bool isFile=false;
 internal FileSystemItem(System.IO.FileSystemInfo obj)
 {
   _obj = obj;
   isFile = obj is System.IO.FileInfo;
 }

 public string Display
 {
   get
   {
     return string.Format("{0} - {1}", isFile ? "FILE" : "[DIR]", _obj.Name);
   }
 }
 public FileSystemItem Data
 {
   get
   {
     return this;
   }
 }

 public void Copy(string location)
 {
   if(!System.IO.Directory.Exists(location))
     System.IO.Directory.CreateDirectory(location);
   if (isFile)
   {
     if (System.IO.File.Exists(location + _obj.Name))
       System.IO.File.Delete(location + _obj.Name);
     System.IO.File.Copy( _obj.FullName, location + _obj.Name);
   }
   else
   {
     if (!System.IO.Directory.Exists(location + _obj.Name))
       System.IO.Directory.CreateDirectory(location + _obj.Name);
     System.IO.DirectoryInfo curr = _obj as System.IO.DirectoryInfo;
     location+= _obj.Name + "\\";
     foreach(System.IO.DirectoryInfo di in curr.GetDirectories())
     {
       FileSystemItem fi = new FileSystemItem(di);
       fi.Copy(location);
     }
     foreach(System.IO.FileInfo fiCurr in curr.GetFiles())
     {
       FileSystemItem fi = new FileSystemItem(fiCurr);
       fi.Copy(location);
     }
   }
 }
}

create a form. . . drop a button, a combobox and a folderbrowse dialog on it. . .

 

make Form1.Main look like this:

[sTAThread]
static void Main() 
{
 Form1 frm = new Form1();
 if (frm.folderBrowserDialog1.ShowDialog() != DialogResult.OK)
   return;
 Application.Run(frm);
}

add this function to the form

private ArrayList AppFileSystemInfo 
{
 get
 { 
   ArrayList result = new ArrayList();
   System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(folderBrowserDialog1.SelectedPath);
   foreach  (System.IO.FileSystemInfo fs in di.GetDirectories())
     result.Add(new FileSystemItem(fs));
   foreach  (System.IO.FileSystemInfo fs in di.GetFiles())
     result.Add(new FileSystemItem(fs));
   return result;
 }
}

attach this Form1.Load:

private void Form1_Load(object sender, System.EventArgs e)
{
 comboBox1.DataSource = AppFileSystemInfo;
 comboBox1.DisplayMember = "Display";
 comboBox1.ValueMember = "Data";
}

atttach this to the button click event:

private void button1_Click(object sender, System.EventArgs e)
{
 FileSystemItem fi = comboBox1.SelectedItem as FileSystemItem;
 fi.Copy("d:\\foobar\\");
}

done.

 

God, I love C#!

Edited by Joe Mamma

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...