C# - 当数据约为 400 MB 时如何处理 Process.StandardOutput 中的数据

发布于 2024-12-07 08:24:47 字数 815 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又怨 2024-12-14 08:24:47

为什么不使用 DirectoryInfo.GetFiles

我猜您现在的大部分时间都被执行的命令占用了,而不是 .NET 代码。将这么多数据按顺序写入流需要 dir 很长时间。然后你使用 String.Split ,它也会被这么多数据阻塞。

通过使用 DirectoryInfo.GetFiles,您应该能够在一行中获取所有文件名(并且您还可以通过这种方式获取有关文件的其他信息):

var files = (new DirectoryInfo(path)
                .GetFiles("*.*", SearchOption.AllDirectories)
                .Select(fi => fi.Name);

如果您确实只关心文件名,则可以使用:

var fileNames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);

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 use String.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):

var files = (new DirectoryInfo(path)
                .GetFiles("*.*", SearchOption.AllDirectories)
                .Select(fi => fi.Name);

If you're really only concerned about filenames, you could use:

var fileNames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
○闲身 2024-12-14 08:24:47

你正在重新发明轮子。添加对 System.IO 的引用并使用 DirectoryInfo 和 FileInfo 类。

You're reinventing the wheel. Add a reference to System.IO and use the DirectoryInfo and FileInfo classes.

近箐 2024-12-14 08:24:47

当您说接收时,您只是指列出目录中的文件吗?

如果是这样,你不能使用Directory.GetFiles()方法吗?

        // Only get files that begin with the letter "c."
        string[] dirs = Directory.GetFiles(@"c:\", "c*");
        Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);
        foreach (string dir in dirs) 
        {
            Console.WriteLine(dir);
        }

来自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?

        // Only get files that begin with the letter "c."
        string[] dirs = Directory.GetFiles(@"c:\", "c*");
        Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);
        foreach (string dir in dirs) 
        {
            Console.WriteLine(dir);
        }

From MSDN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文