privatevoidForm1_Load(objectsender,EventArgse)
{
TreeNoderoot=newTreeNode();
root.Text="目录";
GetFiles(@"E:/Tools",root);
treeView1.Nodes.Add(root);
}
privatevoidGetFiles(stringfilePath,TreeNodenode)
{
DirectoryInfofolder=newDirectoryInfo(filePath);
node.Text=folder.Name;
node.Tag=folder.FullName;
FileInfo[]chldFiles=folder.GetFiles("*.*");
foreach(FileInfochlFileinchldFiles)
{
TreeNodechldNode=newTreeNode();
chldNode.Text=chlFile.Name;
chldNode.Tag=chlFile.FullName;
node.Nodes.Add(chldNode);
}
DirectoryInfo[]chldFolders=folder.GetDirectories();
foreach(DirectoryInfochldFolderinchldFolders)
{
TreeNodechldNode=newTreeNode();
chldNode.Text=folder.Name;
chldNode.Tag=folder.FullName;
node.Nodes.Add(chldNode);
GetFiles(chldFolder.FullName,chldNode);
}
}
C#遍历文件夹构造文件树
最新推荐文章于 2024-08-13 10:38:31 发布