2017-08-24 141 views
0

我有一个TreeView,我需要动态填充。递归填充(winforms)Treeview

内容与目录结构相似(请参阅附图)。

现在,为了获取这些'文件夹',我使用了一个只列出'顶级'文件夹的命令(参考图片)Tree。 (请注意,这不是操作系统目录/文件夹..我只是使用目录/文件夹比喻来使事情可以理解)

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

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

我需要反复发出这些命令,直到我得到的最后一个子..文件夹并使用列表来填充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); 
       } 
      } 
+0

此代码是'PopulateFoldersRecursive'方法吗?你调试了代码吗?任何调试结果?什么是'CallInfaCmd'? –

+0

我认为这个问题已经回答,这个链接可能会帮助你[链接](https://stackoverflow.com/questions/6239544/populate-treeview-with-file-system-directory-structure) – Haxsta

+0

可能重复[填充TreeView与文件系统目录结构](https://stackoverflow.com/questions/6239544/populate-treeview-with-file-system-directory-structure) – GuidoG

回答

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

     }