Why does this code not receive the complete output from an external command?

夙愿已清 提交于 2019-12-11 11:27:43

问题


I am working on a tool to be able to find a PST on a specific drive. This code is taking the project path just because it's for testing purpose.

My problem is that when I try to get the output of the execution of a shell command in an external command processor, I only got the 2 first lines:

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();

My result:

Volume in drive D is Data Volume Serial Number is 7C64-9650

Expected Result:

Volume in drive D is Data Volume Serial Number is 7C64-9650

Directory of D:\PATH 13/12/2012 01:49 PM 1,014,047,744 Archives.pst 4 File(s) 1,355,919,360 bytes

No error message are available.


回答1:


Perhaps another way to skin the cat would be easier? Your current code is not worth the trouble.

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

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

Used like so:

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


来源:https://stackoverflow.com/questions/13868029/why-does-this-code-not-receive-the-complete-output-from-an-external-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!