2013-08-18 69 views
0

我有一個加載選定目錄樹的Treeview。現在我想使目錄有一個文件夾圖像和文件一個文件圖像。文件夾和文件圖像都在連接到我的treeviewimagelist中。我如何調整下面的代碼來做到這一點?將圖像添加到從目錄加載的TreeView中

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


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

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


     foreach (var file in directoryInfo.GetFiles()) 
     { 
      directoryNode.Nodes.Add(new TreeNode(file.Name)); 
     } 
     return directoryNode; 
    } 

回答

1

假設Directory image0指數在imagelistFile image1指數在圖像列表:

private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo) 
{ 
    var directoryNode = new TreeNode(directoryInfo.Name){ImageIndex=0}; 

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


    foreach (var file in directoryInfo.GetFiles()) 
    { 
     directoryNode.Nodes.Add(new TreeNode(file.Name){ImageIndex=1}); 
    } 
    return directoryNode; 
} 
+0

@KingKong謝謝!完美的作品! – daJbot