0

I have a TreeView which I need to populate dynamically.

The contents would be similar to directory structure (Refer attached pic).

Now, in order to fetch these 'folders' I use a command, which would list out only 'top-level' folders (refer pic)Tree. (Please note this is not OS directory / folders.. I am only just using directory / folder analogy to make things understandable)

So, for e.g. I have Root, Folder1, Sub_Folder1, Sub_Folder2, Sub-sub_folder_1, Folder2, then issuing command with a '/' option will give me a list: Folder1, Folder2.

If I need Level-2 folders (Sub_Folder_1 and Sub_Folder_2), I again need to issue the command with option "/Folder1"..

I need to repeatedly issue these commands, until I get the last sub.. folder and use the list to populate a TreeView.

I am using the below C# (4.5) code, but I am able to list only 2-levels.

Any help in correcting would be much appreciated!

try
            {

                BuildInfaCmd(InfaCmdType.ListFolders, folder);

                InfaCmd icmd = CallInfaCmd(InfaCmdExe, InfaCmdArgs);

                if (icmd.ExitCode() == 0)
                {
                    List<string> folders = icmd.GetFolders();

                    if (folders.Count > 0)
                        topFolderFound = true;

                    foreach (string f in folders)
                    {
                        if (node == null) // Add to 'root' of Treeview
                        {
                            TreeNode p = new TreeNode(f);
                            treeView1.Nodes.Add(p);
                            PopulateFoldersRecursive(f, null);
                        }
                        else
                        {
                            callLvl += 1;
                            //MessageBox.Show("Calling recursive " + callLvl.ToString());                            

                            TreeNode p = new TreeNode(f);
                            node.Nodes.Add(p); // Add to calling node as children
                            string fold = node.Text + "/" + f; // The sub-folder to be provided to ListFolder command like -p /RootFolder/SubFolder1/SubFolder2/...
                            PopulateFoldersRecursive(fold, p, callLvl);
                        }
                    }
                }
                else
                {
                    MessageBox.Show(icmd.GetError(), "Error while executing InfaCmd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
sanar
  • 437
  • 9
  • 22
  • This code is of `PopulateFoldersRecursive` method? Did you debug the code? Any findings of debugging? What is `CallInfaCmd`? – Chetan Aug 24 '17 at 13:20
  • I think this question was answered and this link may help you [LINK](https://stackoverflow.com/questions/6239544/populate-treeview-with-file-system-directory-structure) – Haxsta Aug 24 '17 at 13:44
  • Possible duplicate of [Populate TreeView with file system directory structure](https://stackoverflow.com/questions/6239544/populate-treeview-with-file-system-directory-structure) – GuidoG Aug 24 '17 at 14:46

1 Answers1

0

The answers provided were more specific to populating 'Files' / 'Directories'. As conveyed, the 'folders' in my query was not OS-specific, so the answers did not provide much help. I found out a way to recursively add nodes to Treeview.

void PopulateFolders()
        {                  
            int callLvl = 1;
            BuildInfaCmd(InfaCmdType.ListFolders);    

            int timeout = 60000;
            if (lstProjects.SelectedItem.ToString().ToLower().StartsWith("hdp"))
                timeout = 600000;

            InfaCmd icmd = CallInfaCmd(InfaCmdExe, InfaCmdArgs,null,timeout);    

            if (icmd.ExitCode() == 0)
            {
                List<string> folders = icmd.GetFolders();

                foreach (string f in folders)
                {
                    TreeNode p = new TreeNode(f);
                    treeView1.Nodes.Add(p);
                    PopulateFoldersRecursive(f, p, 1);
                }

                lstFolders.DataSource = folders;
            }
            else
            {
                MessageBox.Show(icmd.GetError(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

void PopulateFoldersRecursive(string folder, TreeNode node, [Optional]int callLevel)
        {
            int timeout = 60000;
            if (lstProjects.SelectedItem.ToString().ToLower().StartsWith("hdp"))
                timeout = 600000;
            int callLvl = callLevel;
            string fold = "";                
            try
            {                   

                BuildInfaCmd(InfaCmdType.ListFolders, folder);
                InfaCmd icmd = CallInfaCmd(InfaCmdExe, InfaCmdArgs,null, timeout);     
                if (icmd.ExitCode() == 0)
                {
                    List<string> folders = icmd.GetFolders();                               

                    if (folders.Count > 0)
                        topFolderFound = true;

                    foreach (string f in folders)
                    {                            
                            callLvl += 1;
                            //MessageBox.Show("Calling recursive " + callLvl.ToString());                            

                            TreeNode p = new TreeNode(f);
                            node.Nodes.Add(p); // Add to calling node as children
                                               // MessageBox.Show(callLvl.ToString() + "; Node.text : " + node.Text + " ;  f : " + f);
                            dirTree.Add(p.FullPath);
                            if (String.IsNullOrEmpty(folderFullPath))
                            {
                                //fold = node.Text + "/" + f; // The sub-folder to be provided to ListFolder command like -p /RootFolder/SubFolder1/SubFolder2/...
                                fold = folder + "/" + f; // ORIGINAL                                   
                                folderFullPath = fold;
                            }
                            else
                            {                                   

                                fold = folder + "/" + f; // TEST

                                folderFullPath = fold; // ORIGINAL
                            }

                            PopulateFoldersRecursive(fold, p, callLvl);
                        }
                    }
                }

                else
                {
                    MessageBox.Show(icmd.GetError(), "Error while executing InfaCmd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }    

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException, "Error");
            }

        }
sanar
  • 437
  • 9
  • 22