Reputation: 1863
I'm trying to capture the output from other applications. Capturing the output from ping works well. The variable output contains the expected output.
var p = new Process();
p.StartInfo.FileName = "ping";
p.StartInfo.Arguments = "www.google.com";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
But when I use this code for capturing the output of expdp (which is an oracle tool for exports), the variable is empty. Runnig the same command in the console will return some output.
p.StartInfo.FileName = "expdp";
p.StartInfo.Arguments = "help=y";
Am I missing something ?
Upvotes: 3
Views: 616
Reputation: 19873
I had this problem once. The most recent answer does make sense, but I haven't tested it as it appeared 6 months after I encountered the problem. Basically the problem seems to be that ReadToEnd() reads at a precise moment, just after p.Start(), where nothing has been output to the screen yet. You can check it out by putting a long sleep between the start and the ReadToEnd().
Upvotes: 1
Reputation: 31204
Try checking the StandardError
stream and see if you get anything there
var p = new Process();
p.StartInfo.FileName = "expdp";
p.StartInfo.Arguments = "help=y";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
var error = p.StandardError.ReadToEnd();
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
one thing to note, If either your output stream or your error stram is too long, than this approach can cause deadlocks.
If that is the case, you'll have to read one of the streams asynchronously.
Upvotes: 7