开发者

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

开发者 https://www.devze.com 2023-03-26 17:35 出处:网络
In my Winform application, I need to call a DOS or Console application and I do that with the following code:

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 clo开发者_高级运维ses 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消