0

I'm not sure whether this topics has been disscussed before or not, but I'm not sure the exact word to search for it. What method/class should I use?

The program has 3 buttons: 1) for folder browsing, 2) scan for the selected folder content, and 3) open the file. When user browse the selected folder**(1), user click scan button to scan from the first file until the last available files and listed it text box(2)** and from that user can decide whether to open the files or not**(3)**.

Here are what have I done so far (no 1 and 3):

//For browse.
private void browse2()
{
    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
     {
        this.txtDest.Text = folderBrowserDialog1.SelectedPath;
     }
}

//For opening folder.
private void btnOpen_Click(object sender, EventArgs e)
{
    try
    {
        Process.Start(txtDest.Text);
    }
    catch
    {
        MessageBox.Show("Please select one file/folder");
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user147685
  • 471
  • 7
  • 16
  • 28
  • 1
    Are you trying to replicate the functionality of the OpenFileDialog? http://msdn.microsoft.com/en-us/library/aa287592(VS.71).aspx – Byron Ross Sep 16 '09 at 06:55
  • Why don't you use the FolderBrowser of .NET to browse te folders? – Natrium Sep 16 '09 at 06:55
  • This is a very strange question, certainly if you see one of your previous questions: http://stackoverflow.com/questions/1420408/c-how-to-browse-for-folder – Natrium Sep 16 '09 at 06:57
  • Dear Byron Ross, Yes it similar but i want to list out the content first. then after that, the selection of the file is done by the btnOpen(). – user147685 Sep 16 '09 at 08:17

2 Answers2

0

Well my example is a WPF app that adds files/ folders in a directory to a treeview, but you should get the general idea:

Note: Code was written for a training exercise, and therefore only goes 3 levels deep, as a proof of concept kind of thing


 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (DriveInfo di in DriveInfo.GetDrives())
            {
                TreeViewItem drive = new TreeViewItem();
                drive.Header = di.Name;
                treeView1.Items.Add(drive);

                DirectoryInfo folders = new DirectoryInfo(di.Name);

                // The depth count means that it only goes 3 levels deep, to make it quick to load
                GetFoldersAndFiles(drive, folders, 3);
            }
        }

        private static void GetFoldersAndFiles(TreeViewItem parent, DirectoryInfo folders, int depth)
        {        
            if ((depth > 0)
            {
                foreach (DirectoryInfo dirI in folders.GetDirectories())
                {
                    TreeViewItem dir = new TreeViewItem();
                    dir.Header = dirI.Name;
                    parent.Items.Add(dir);

                    GetFoldersAndFiles(dir, dirI, depth - 1);
                }

                foreach (FileInfo fileI in folders.GetFiles())
                {
                    TreeViewItem file = new TreeViewItem();
                    file.Header = fileI.Name;
                    parent.Items.Add(file);
                }
            }
        }
Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
  • Be ware that a solution that calls itself does work, but if you check enough folders you will get a stackoverflow ( No pun intended ). – EKS Sep 16 '09 at 07:45
0

If you are trying to just open a file you can directly use an Open File Dialog.

If you need to display the contents of a directory you can use the Directory Info Class.

TJB
  • 13,367
  • 4
  • 34
  • 46