c#视图文件树的形式

本文关键字:视图 文件 | 更新日期: 2023-09-27 18:10:29

是否有任何方法可以直接在窗体上查看文件系统,具有与Visual Studio c#中的OpenFileDialog(打开文件夹,选择文件)类似的功能?

c#视图文件树的形式

试试这个:

private void Button1_Click(object sender, EventArgs e)
{
     ListDirectory("Your TreeView Name here", "root path")
}
private void ListDirectory(TreeView tv, string path)
{
    tv.Nodes.Clear();
    var rootDirectoryInfo = new DirectoryInfo(path);
    tv.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;
}