I am trying to retrieve list of files from a server with the windows command – “DIR /S/B”
The output is huge (around 400 MB). Now when I tried retrieve it with below approach, its taking hours to process. Is there any faster way to do it.
string path = args[0];
var start = DateTime.Now;
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "dir /s/b " + path );
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
//string [] result = proc.StandardOutput.ReadToEnd().Split('\n'); ;
StreamWriter writer = new StreamWriter("FileList.lst");
while (proc.StandardOutput.EndOfStream != true)
{
writer.WriteLine(proc.StandardOutput.ReadLine());
writer.Flush();
}
writer.Close();
Why not use
DirectoryInfo.GetFiles?I’m guessing quite a bit of your time now is being eaten up by the command executing, not the .NET code. It’ll take
dira long time to write that much data to a stream in sequence. You then useString.Splitwhich is also going to choke on that much data.By using DirectoryInfo.GetFiles, you should be able to get all the file names in a single line (and you could also get other information about the files this way):
If you’re really only concerned about filenames, you could use: