Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#查看表单上的文件树_C#_Winforms - Fatal编程技术网

C#查看表单上的文件树

C#查看表单上的文件树,c#,winforms,C#,Winforms,是否有任何方法可以直接在窗体上查看文件系统,其功能与Visual Studio C#中的OpenFileDialog(打开文件夹,选择文件)类似?尝试以下操作: private void Button1_Click(object sender, EventArgs e) { ListDirectory("Your TreeView Name here", "root path") } private void ListDirectory(TreeView tv, string pat

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

尝试以下操作:

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;
}

是的,当然有。如果您询问是否可以在窗体上放置内置控件,则答案为否。否则,方法是使用
System.IO
文件
目录
类在驱动器上的条目中递归,我建议延迟加载。没有什么比尝试实现自己的文件浏览器的应用程序更让我烦恼的了。这是一种跨线程冲突,原则上是正确的,但使用根路径之类的东西会导致应用程序在递归加载整个驱动器时停止响应。如果文件夹路径足够深,还可能出现堆栈溢出异常。