我想把指定E盘目录下FOX文件夹里包含的文件夹和文件都显示出来,下面的程序可以把所有的文件夹显示在treeView里,帮一下我,看还应该怎么写,才能把文件也显示出来。谢了
namespace 文件列表
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (!Directory.Exists("E:\\FOX"))
{
Directory.CreateDirectory("E:\\FOX");
}
PopulateTreeView();
}
private void Form1_Load(object sender, EventArgs e)
{
treeView1.ExpandAll();
}
private void GetDirectories(DirectoryInfo[] subDirs,
TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs;
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir;
subSubDirs = subDir.GetDirectories();
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
//foreach (FileInfo f in info.GetFiles("*.*")) //查找文件
//{
// nodeToAddTo.Nodes.Add(info + f.ToString());
//}
}
private void PopulateTreeView()
{
TreeNode rootNode;
DirectoryInfo info = new DirectoryInfo(@"E:\EOX");
if (info.Exists)
{
rootNode = new TreeNode(info.Name);
rootNode.Tag = info;
GetDirectories(info.GetDirectories(), rootNode);
treeView1.Nodes.Add(rootNode);
}
}
}
}