代码改变世界

读取一个大文件夹下的所有文件,包括子文件夹中的文件,并且读入文本框中

2012-09-13 11:09  田志良  阅读(140)  评论(0编辑  收藏  举报

private void button1_Click(object sender, EventArgs e)
{
this.textBox2.Text = "";
string FilePath = this.textBox1.Text;


if (Directory.Exists(FilePath))
{
DirectoryInfo d = new DirectoryInfo(FilePath);
ReadFile(d.GetFiles());

DirectoryInfo[] dir = d.GetDirectories();
ReadDir(dir);

}
}

private void ReadDir(DirectoryInfo[] dir)
{
foreach (DirectoryInfo d in dir)
{
ReadFile(d.GetFiles());
ReadDir(d.GetDirectories());
}
}

private void ReadFile(FileInfo[] fileList)
{
string _str = "";
foreach (FileInfo _file in fileList)
{
if (_file.Extension == ".cs")
{
_str += File.ReadAllText(_file.FullName);
}
}
this.textBox2.Text += _str;
}