Click here to Skip to main content
15,883,829 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
I want to automatically find all directories in a local machine using c#.
I have done this with the code shown below, which should select a node to find sub directory and I want to do this automatically.

Can anyone help me?

C#
private void button2_Click(object sender, EventArgs e)
{

            DriveInfo[] varDriveInfo = DriveInfo.GetDrives();

            string[] SubElement = new string[varDriveInfo.Length];
            TreeNode trd=new TreeNode();
            for (int i = 0; i < varDriveInfo.Length; i++) 
            {
                trd.Nodes.Add(varDriveInfo[i].ToString().Substring(0, 1));

                treeView1.Nodes.Add(varDriveInfo[i].ToString().Substring(0, 1));
                try
                {
                    string[] subDirectory = Directory .GetDirectories (varDriveInfo[i] .ToString());
                    for (int j = 0; j < subDirectory.Length; j++)
                    {
                        treeView1.Nodes[i].Nodes.Add(subDirectory[j]);
                    }
                }
                catch
                { continue; }
            }
            
        }

code for find sub directory :

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
 {
     try
     {
         string[] subDirectory = Directory.GetDirectories(treeView1.SelectedNode.Text);

         string[] ListFile = Directory.GetFiles(treeView1.SelectedNode.Text);

         listBox1.Items.Clear();

         foreach (string FileName in ListFile)
             listBox1.Items.Add(FileName);

         for (int j = 0; j < subDirectory.Length; j++)
             try
             {
                 treeView1.SelectedNode.Nodes.Add(subDirectory[j]);
             }


             catch
             { continue; }
     }
     catch { }

 }


[Edit - Spelling, grammar, and added code block]
Posted
Updated 6-Mar-12 3:41am
v2
Comments
Bojjaiah 6-Mar-12 9:31am    
add pre tag

use DirectoryInfo class. See here[^] for information and a good example.
 
Share this answer
 
You should use recursion to find everything from a starting point of C:\

http://support.microsoft.com/kb/303974[^]

Have a search for Directory Recursion c#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900