C# - 当数据约为 400 MB 时如何处理 Process.StandardOutput 中的数据
我正在尝试使用 Windows 命令“DIR /S/B”从服务器检索文件列表 输出很大(大约 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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用
DirectoryInfo.GetFiles
?我猜您现在的大部分时间都被执行的命令占用了,而不是 .NET 代码。将这么多数据按顺序写入流需要
dir
很长时间。然后你使用 String.Split ,它也会被这么多数据阻塞。通过使用 DirectoryInfo.GetFiles,您应该能够在一行中获取所有文件名(并且您还可以通过这种方式获取有关文件的其他信息):
如果您确实只关心文件名,则可以使用:
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
dir
a long time to write that much data to a stream in sequence. You then useString.Split
which 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:
你正在重新发明轮子。添加对 System.IO 的引用并使用 DirectoryInfo 和 FileInfo 类。
You're reinventing the wheel. Add a reference to System.IO and use the DirectoryInfo and FileInfo classes.
当您说接收时,您只是指列出目录中的文件吗?
如果是这样,你不能使用Directory.GetFiles()方法吗?
来自MSDN
When you say recieve, do you simply mean list the files in the directory?
If so, can you not use the Directory.GetFiles() method?
From MSDN