C#如何遍历一个文件夹下面所有的文件、包括子文件夹里面的文件?
ArrayList GetAll(DirectoryInfo dir)//搜索文件夹中的文件
{
ArrayList FileList = new ArrayList();
FileInfo[] allFile = dir.GetFiles();
foreach (FileInfo fi in allFile)
{
FileList.Add(fi.Name);
}
DirectoryInfo[] allDir= dir.GetDirectories();
foreach (DirectoryInfo d in allDir)
{
GetAll(d);
}
return FileList;
}
DirectoryInfo d = new DirectoryInfo(@"F:\");
ArrayList Flst= GetAll(d);
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
namespace bianli
{
class Program
{
static ArrayList FileList = new ArrayList();
static ArrayList GetAll(DirectoryInfo dir)//搜索文件夹中的文件
{
FileInfo[] allFile = dir.GetFiles();
foreach (FileInfo fi in allFile)
{
FileList.Add(fi.FullName);
}
DirectoryInfo[] allDir = dir.GetDirectories();
foreach (DirectoryInfo d in allDir)
{
GetAll(d);
}
return FileList;
}
static void Main(string[] args)
{
DirectoryInfo d = new DirectoryInfo(@"E:\test");
ArrayList Flst = GetAll(d);
foreach (string f in Flst)
{
Console.WriteLine(f);
}
}
}
}