1

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.

user7116
  • 63,008
  • 17
  • 141
  • 172
  • I have tried on my PC. Works fine. I saw "Volume in drive D is Data Volume Serial Number is 7CXX-XXXX". But I used Console.WriteLine(output) instead of MessageBox.Show(output) – Roman Melnyk Dec 13 '12 at 20:39
  • Shouldn't you be doing the `string output = p.StandardOutput.ReadToEnd();` after the `p.WaitForExit();`? – nick_w Dec 13 '12 at 20:43
  • 1
    @nick_w - no, that risks a deadlock. It's discussed in the docs: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx – Brian Reischl Dec 13 '12 at 20:47

1 Answers1

3

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));
user7116
  • 63,008
  • 17
  • 141
  • 172
  • Hey, thanks for your prompt answer. I tried and it works perfectly.. Thanks à lot! – JF Brouillette Dec 13 '12 at 20:52
  • 1
    @user1902146: No problem! The .Net Framework is *huge* and thus if you feel like you have to *run another program* to accomplish a goal, you probably don't. The trick is finding the functionality in MSDN :) – user7116 Dec 13 '12 at 20:54