为什么这个代码不接受外部指令的完整输出?(Why does this code not receiv

2019-10-17 12:26发布

我在一个工具的工作,以便能够找到一个PST特定的驱动器上。 此代码正在只是因为它是用于测试目的的项目路径。

我的问题是,当我试图得到一个shell命令的执行输出在外部指令的处理器,我只得到了2个第一行:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C dir /s *.pst";
p.Start();
string output = p.StandardOutput.ReadToEnd();
MessageBox.Show(output);
p.WaitForExit();

我的结果:

在驱动d体积数据卷序列号是7C64-9650

预期结果:

在驱动d体积数据卷序列号是7C64-9650

d目录:\ PATH 13/12/2012下午1时49分1014047744 Archives.pst 4文件(S)1355919360个字节

没有错误信息是可用的。

Answer 1:

也许是另一种方法对皮肤的猫会更容易? 您当前的代码是不值得的麻烦。

// .Net 2.0
string[] psts = Directory.GetFiles(".", "*.pst", SearchOption.AllDirectories);

// .Net 4.0+
var psts = Directory.EnumerateFiles(".", "*.pst", SearchOption.AllDirectories);

使用像这样:

MessageBox.Show(String.Join(", ", psts));


文章来源: Why does this code not receive the complete output from an external command?