2017-04-22 109 views
1

如何修改此代碼以跳過創建根節點並僅創建其他節點?如何跳過在樹形視圖上創建根節點?

void ListDirectory(TreeView treeView, string path) 
    { 
     treeView.Nodes.Clear(); 
     var rootDirectoryInfo = new DirectoryInfo(path); 
     treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo)); 
    } 


    TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo) 
    { 
     var directoryNode = new TreeNode(directoryInfo.Name); 

     foreach (var directory in directoryInfo.GetDirectories()) 
     { 
      directoryNode.Nodes.Add(CreateDirectoryNode(directory)); 
     } 

     return directoryNode; 
    } 

回答

0

像這樣:

void ListDirectory(TreeView treeView, string path) 
{ 
    treeView.Nodes.Clear(); 
    var rootDirectoryInfo = new DirectoryInfo(path); 

    foreach (var directory in rootDirectoryInfo.GetDirectories()) 
    { 
     treeView.Nodes.Add(CreateDirectoryNode(directory)); 
    } 
} 

TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo) 
{ 
    var directoryNode = new TreeNode(directoryInfo.Name); 

    foreach (var directory in directoryInfo.GetDirectories()) 
    { 
     directoryNode.Nodes.Add(CreateDirectoryNode(directory)); 
    } 

    return directoryNode; 
} 
+0

它的工作,謝謝。這是一個優雅的解決方案。 – Caladan