为什么我的这个TreeView只能获得根目录下的第1层文件啊??
private void FilltvwDirectory()
{
//得到系统盘符
string [] drives = Environment.GetLogicalDrives();
for(int i = 0;i<drives.Length;i++)
{
//添加系统盘符
TreeNode cRoot = new TreeNode(drives[i]);
treeView1.Nodes.Add(cRoot);
AddDirectories(cRoot); //添加系统盘下的文件
}
}
private void AddDirectories(TreeNode node)
{
try
{
//获得当前截点路径
DirectoryInfo dir = new DirectoryInfo(GetPathFromNode(node));
//获取当前截点所在路径下的子文件夹
DirectoryInfo[] e = dir.GetDirectories();
for (int i = 0;i<e.Length;i++)
{
string name = e[i].Name;
if(!name.Equals(".")&&!name.Equals(".."))
{
node.Nodes.Add(new TreeNode(name));
}
}
}
catch(Exception se)
{
MessageBox.Show(se.Message);
}
}
private string GetPathFromNode(TreeNode node)
{
if (node.Parent == null)
{
return node.Text;
}
return Path.Combine(GetPathFromNode(node.Parent),node.Text); //合并父截点与相对应的子截点路径
}
private void tvwDirectory_BeforeExpand(object soure,TreeViewCancelEventArgs e)
{
TreeNode nodeExpandint = (TreeNode)e.Node;
AddSubDirectoties(nodeExpandint);
}
private void AddSubDirectoties(TreeNode node)
{
for (int i = 0;i<node.Nodes.Count;i++)
AddDirectories(node.Nodes[i]);
}