2017-08-24 139 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"); 
      } 

     }