user850010
user850010

Reputation: 6359

How to prevent DOS/Console application that was called using Process class in .NET, from automaticly closing the command prompt after finish?

In my Winform application, I need to call a DOS or Console application and I do that with the following code:

    Process proc = new Process();
    proc.StartInfo.FileName = lLocation.Text+"\\pywin32.exe";
    proc.StartInfo.Arguments = lLocation.Text+"\\data.pos";
    proc.Start();

Problem is that after the application outputs the result on the screen, the Command Prompt closes immediately, so I can not read the result of that application.

One solution would probably be to create .bat file with "pause" command in the end and use that in the Process class, but I am wondering, if there is another way?

Upvotes: 2

Views: 2281

Answers (2)

Serge Wautier
Serge Wautier

Reputation: 21878

It's hard to control the lifetime of the console of another process. Your batch file with a pause looks an easy trick to solve the problem. Unless you want to go the Brandon way.

Upvotes: 0

Brandon Moretz
Brandon Moretz

Reputation: 7621

You could just read the output of the process:

Process proc = new Process();

proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = lLocation.Text+"\\pywin32.exe";
proc.StartInfo.Arguments = lLocation.Text+"\\data.pos";
proc.Start();

string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();

Then do whatever you wish with the output.

Upvotes: 4

Related Questions