源码下载地址: http://pan.baidu.com/s/1c2cSLgc 密码: 51mc
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TreeView遍历目录及文件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = AppDomain.CurrentDomain.BaseDirectory+ @"\1";//这个目录要存在如没有自己添加,再加子目录文件等
FileIndex(path, treeView1.Nodes);
}
public void FileIndex(string fileIndex, TreeNodeCollection treeNodes)
{
try
{
string[] fileIndexPath = Directory.GetDirectories(fileIndex);
foreach (string temp in fileIndexPath)
{
TreeNode node = treeNodes.Add(Path.GetFileName(temp));
FileIndex(temp, node.Nodes);//循环添加到没有子目录为止,注意有的像C盘里面不能防问的目录就会出问题,可以用try catch
}
string[] fileName = Directory.GetFiles(fileIndex);
foreach (string temp in fileName)
{
treeNodes.Add(Path.GetFileName(temp));
}
}
catch
{
}
}
}
}