0

我有一个Treeview加载选定目录的树。现在我想让目录有一个文件夹图像,文件有一个文件图像。文件夹和文件图像都imagelist连接到我的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;
    }
4

1 回答 1

1

Suppose the Directory image has index of 0 in your imagelist and File image has index of 1 in your imagelist:

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;
}
于 2013-08-18T16:14:37.180 回答