External Process will not end correctly

G

Guest

Hi Guys,

I am trying to caputre the output for an external application. The idea is
to use the System.Diagnostics.Process to run the exe in a process and
redirect the output to a string.

When I run the external command it works fine. But when i wrap it in a .net
exe it fails to exit and the application does not exit. If I kill the
external exe from task manager, the .net wrap completes.

If I proc.StartInfo.UseShellExecute to true, i can get the external
application to run, but then i lose the output.

Here is a snippet of the code.

static void Main(string[] args)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.FileName=@"C:\Program.exe";
proc.StartInfo.Arguments="";
proc.Start();
proc.WaitForExit();
string stdoutput = proc.StandardOutput.ReadToEnd();
string stderror = proc.StandardError.ReadToEnd();
Console.WriteLine (stdoutput);
Console.WriteLine (stderror);
}

Cheers

Craig.
 
G

Guest

Hi guys,

Got it working, just had to move the proc.WaitForExit(); line to below the
read lines.

Cheers

Craig.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top