Directory Listing with TreeView

I’m trying to enumerate the directories and sub directories in a Windows Form using a Tree View. I can view the directories but when I want to see the files in the sub directories I only see the files from the root. :confused:


private void button1_Click(object sender, EventArgs e)
        {         

            try
            {
                string path = @"e:\\\\";

                TreeNode rootNode = treeView1.Nodes.Add("e:\\\\");

                DirectoryInfo a = new DirectoryInfo(path);
                FileInfo[] cc = a.GetFiles("*.*");

                foreach (DirectoryInfo b in a.GetDirectories())
                {
                    TreeNode aa = new TreeNode(b.FullName);
                    rootNode.Nodes.Add(aa);
                    //          TreeNode bb = new TreeNode(cc.FullName);
                    //          aa.Nodes.Add(bb);
                    foreach (FileInfo dd in a.GetFiles())
                    {
                        TreeNode bb = new TreeNode(dd.FullName);
                        aa.Nodes.Add(bb);
                    }
                }
            }
            catch (UnauthorizedAccessException c)
            {
                txtError.Text += c.Message;
            }
            catch (IOException d)
            {
                txtError.Text += d.Message;
            }
        }

Here is your problem:


string path = @"e:\\\\";
 
TreeNode rootNode = treeView1.Nodes.Add("e:\\\\");
 
DirectoryInfo a = new DirectoryInfo(path);
FileInfo[] cc = a.GetFiles("*.*");


Your “DirectoryInfo” object will always call the “path” variable, which is static at e:\\.

You need a way to update the current directory. You could create a label with the current directory, then put that value into the DirectoryInfo variable.