我試圖從服務器檢索文件列表與Windows命令 - 「DIR/S/B」 輸出是巨大的400 MB)。現在,當我嘗試用以下方法檢索它時,需要花費幾個小時來處理。有沒有更快的方法來做到這一點。C# - 如何處理來自Process.StandardOutput數據大約400 MB時的數據
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();
我試過DirectoryInfo的方法,但它未能得到一些在windows目錄下的文件夾中。它給出了一個例外..拒絕訪問文件/文件夾。而DIR運行平穩的文件/文件夾。 –
@MayurJ我以前有過這個問題,這裏是我發佈的問題,也許答案會幫助你這個http://stackoverflow.com/questions/6762101/c-file-permission-exclusion –
在.Net 4.0或更高版本,推薦使用EnumerateFiles而不是GetFiles,因爲GetFiles在整個數組填滿之前不會返回,而使用EnumerateFiles則可以立即開始迭代文件。 –