Directory Question

lothos12345

Well-known member
Joined
May 2, 2002
Messages
294
Location
Texas
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.
 
start with a class like this (C# because vb is mess) . . .
C#:
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:
C#:
[STAThread]
static void Main() 
{
  Form1 frm = new Form1();
  if (frm.folderBrowserDialog1.ShowDialog() != DialogResult.OK)
    return;
  Application.Run(frm);
}
add this function to the form
C#:
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:
C#:
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:
C#:
private void button1_Click(object sender, System.EventArgs e)
{
  FileSystemItem fi = comboBox1.SelectedItem as FileSystemItem;
  fi.Copy("d:\\foobar\\");
}
done.

God, I love C#!
 
Last edited by a moderator:
Back
Top