下面是我的代码
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);
}
}
}
当我运行时,我只是得到一个空白的树视图表单?无法弄清楚错误是什么?
顺便说一句,这是我在 Stack Overflow 上的第一篇文章。