我正在尝试使用以下方法列出根目录的所有子目录中的文件。但是当文件数量以百万计时,它需要很长时间。有没有更好的方法来做到这一点。
我正在使用.NET 3.5,所以不能使用枚举器:-(
******************* Main *************
DirectoryInfo dir = new DirectoryInfo(path);
DirectoryInfo[] subDir = dir.GetDirectories();
foreach (DirectoryInfo di in subDir) //call for each sub directory
{
PopulateList(di.FullName, false);
}
*******************************************
static void PopulateList(string directory, bool IsRoot)
{
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "dir /s/b \"" + directory + "\"");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string fileName = directory.Substring(directory.LastIndexOf('\\') + 1);
StreamWriter writer = new StreamWriter(fileName + ".lst");
while (proc.StandardOutput.EndOfStream != true)
{
writer.WriteLine(proc.StandardOutput.ReadLine());
writer.Flush();
}
writer.Close();
}