Scott Stewart

Scott Stewart

  • 1.5k
  • 189
  • 18.1k

Routine is running twice

Mar 12 2012 6:54 PM
Could someone tell me why this routine runs twice?

       private void PopulateTreeView(string county)
        {
            TreeNode rootNode;
            DirectoryInfo info = new DirectoryInfo(@"C:\Users\Scot\Documents\Landman\Runsheets\" +county);
            if (info.Exists)
            {
                rootNode = new TreeNode(info.Name);
                rootNode.Tag = info;
                GetDirectories(info.GetDirectories(), rootNode);
                treeView1.Nodes.Add(rootNode);
            }
        }

        private void GetDirectories(DirectoryInfo[] subDirs,TreeNode nodeToAddTo)
        {
            TreeNode aNode;
            DirectoryInfo[] subSubDirs;
            foreach (DirectoryInfo subDir in subDirs)
            {
                aNode = new TreeNode(subDir.Name, 0, 0);
                aNode.Tag = subDir;
                aNode.ImageKey = "folder";
                subSubDirs = subDir.GetDirectories();
                if (subSubDirs.Length != 0)
                {
                    GetDirectories(subSubDirs, aNode);
                }
                nodeToAddTo.Nodes.Add(aNode);
            }
        }

I got his from an article on how to populate a treeview. The only change I made was to add a parameter to the call so I could limit the contents of the treeview. But it runs twice. When it finishes there are two top level nodes with the same name and all of the sub nodes are the same.

Thanks for your help.

Answers (6)