C# 如何在 TreeView 中显示目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16315042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display directories in a TreeView?
提问by shahul ha
Below is my code
下面是我的代码
DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer");
private void Form1_Load(object sender, EventArgs e)
{
if (Directory.Exists("FileExplorer"))
{
try
{
DirectoryInfo[] directories = directoryInfo.GetDirectories();
foreach (FileInfo file in directoryInfo.GetFiles())
{
if (file.Exists)
{
TreeNode nodes = treeView.Nodes[0].Nodes.Add(file.Name);
}
}
if (directories.Length > 0)
{
foreach (DirectoryInfo directory in directories)
{
TreeNode node = treeView.Nodes[0].Nodes.Add(directory.Name);
node.ImageIndex = node.SelectedImageIndex = 0;
foreach (FileInfo file in directory.GetFiles())
{
if (file.Exists)
{
TreeNode nodes = treeView.Nodes[0].Nodes[node.Index].Nodes.Add(file.Name);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
When I run I just get a blank treeview form? Unable to figure out what is the error?
当我运行时,我只会得到一个空白的树形视图表单?无法弄清楚是什么错误?
Btw this my first post in Stack Overflow.
顺便说一句,这是我在 Stack Overflow 上的第一篇文章。
采纳答案by Himanshu Kumar
This should solve your problem, I tried on WinForm though:
这应该可以解决您的问题,不过我在 WinForm 上尝试过:
public Form1()
{
InitializeComponent();
DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\hikuma\Documents\IR");
if (directoryInfo.Exists)
{
treeView1.AfterSelect += treeView1_AfterSelect;
BuildTree(directoryInfo, treeView1.Nodes);
}
}
private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe)
{
TreeNode curNode = addInMe.Add(directoryInfo.Name);
foreach (FileInfo file in directoryInfo.GetFiles())
{
curNode.Nodes.Add(file.FullName, file.Name);
}
foreach (DirectoryInfo subdir in directoryInfo.GetDirectories())
{
BuildTree(subdir, curNode.Nodes);
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if(e.Node.Name.EndsWith("txt"))
{
this.richTextBox1.Clear();
StreamReader reader = new StreamReader(e.Node.Name);
this.richTextBox1.Text = reader.ReadToEnd();
reader.Close();
}
}
It is a simple example of how you can open file in rich text box, it can be improved a lot :). You might want to mark as answer or vote up if it helped :) !!
这是一个如何在富文本框中打开文件的简单示例,它可以改进很多:)。如果有帮助,您可能想标记为答案或投票:) !!
回答by Hendrik T
Try this: (note make sure your directoryInfo location contains some folders)
试试这个:(注意确保你的 directoryInfo 位置包含一些文件夹)
DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer");
private void Form1_Load(object sender, EventArgs e)
{
if (directoryInfo.Exists)
{
try
{
treeView.Nodes.Add(directoryInfo.Name);
DirectoryInfo[] directories = directoryInfo.GetDirectories();
foreach (FileInfo file in directoryInfo.GetFiles())
{
if (file.Exists)
{
TreeNode nodes = treeView.Nodes[0].Nodes.Add(file.Name);
}
}
if (directories.Length > 0)
{
foreach (DirectoryInfo directory in directories)
{
TreeNode node = treeView.Nodes[0].Nodes.Add(directory.Name);
node.ImageIndex = node.SelectedImageIndex = 0;
foreach (FileInfo file in directory.GetFiles())
{
if (file.Exists)
{
TreeNode nodes = treeView.Nodes[0].Nodes[node.Index].Nodes.Add(file.Name);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
回答by teej
DirectoryInfo.Exists("FileExplorer") will check for "C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\debug\FileExplorer", not "C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer", when you are running in debug mode.
DirectoryInfo.Exists("FileExplorer") 将检查“C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\debug\FileExplorer”,而不是“C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer”,当您在调试模式下运行时。
回答by Chemass
Try the following:
请尝试以下操作:
private void Form1_Load(object sender, EventArgs e)
{
if (directoryInfo.Exists)
{
try
{
treeView.Nodes.Add(LoadDirectory(directoryInfo));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private TreeNode LoadDirectory(DirectoryInfo di)
{
if (!di.Exists)
return null;
TreeNode output = new TreeNode(di.Name, 0, 0);
foreach (var subDir in di.GetDirectories())
{
try
{
output.Nodes.Add(LoadDirectory(subDir));
}
catch (IOException ex)
{
//handle error
}
catch { }
}
foreach (var file in di.GetFiles())
{
if (file.Exists)
{
output.Nodes.Add(file.Name);
}
}
return output;
}
}
It's better to split out the directory parsing into a recursive method so that you can go all the way down the tree.
最好将目录解析拆分为递归方法,以便您可以一直沿着树向下走。
This WILL block the UI until it's completely loaded - but fixing that is beyond the scope of this answer...
这将阻止 UI 直到它完全加载 - 但修复超出了这个答案的范围......
:)
:)

