问题
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