gpt4 book ai didi

c# - 如何在 TreeView 中显示目录?

转载 作者:太空狗 更新时间:2023-10-29 21:23:04 26 4
gpt4 key购买 nike

<分区>

下面是我的代码

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

当我运行时,我只得到一个空白的 TreeView 表单?无法弄清楚错误是什么?

顺便说一句,这是我在 Stack Overflow 中的第一篇文章。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com