I am trying to list the files in all the sub-directories of a root directory with the below approach. But its taking much time when the number of files are in millions. Is there any better approach of doing this.
I am using .NET 3.5 so can't use enumerator :-(
******************* 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();
}