递归填充(winforms)Treeview

时间:2017-08-24 13:05:39

标签: c# winforms treeview

我有一个TreeView,我需要动态填充。

内容与目录结构类似(参见附图)。

现在,为了获取这些“文件夹”,我使用了一个命令,该命令仅列出“顶级”文件夹(参见图片)Tree。 (请注意这不是OS目录/文件夹..我只是使用目录/文件夹类比来使事情变得可以理解)

因此,例如我有Root,Folder1,Sub_Folder1,Sub_Folder2,Sub-sub_folder_1,Folder2,然后用'/'选项发出命令会给我一个列表:Folder1,Folder2。

如果我需要Level-2文件夹(Sub_Folder_1和Sub_Folder_2),我再次需要发出带有“/ Folder1”选项的命令..

我需要重复发出这些命令,直到我得到最后一个子文件夹并使用该列表填充TreeView。

我正在使用下面的C#(4.5)代码,但我只能列出2个级别。

任何帮助纠正将非常感谢!

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);
                }
            }

1 个答案:

答案 0 :(得分:0)

提供的答案更具体地填充'文件'/'目录'。传达后,我的查询中的'文件夹'不是特定于操作系统的,因此答案没有提供太多帮助。我找到了一种递归方式向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");
            }

        }