Getting the ouput from Console window into Winform application

假装没事ソ 提交于 2019-12-11 08:06:13

问题


I have a console application which does a set of operations and gives out messages after completion of each operation. When I run my console app, the messages in my console window may look like this:

Checking prerequisites...
Completing prerequisites..
Performing installation...
Completing installation...
Done..!

Now I'm executing this console application from one of my C# windows applications by using Process.StartInfo(). I need to get all the messages thrown by my console application to be displayed in the windows form of my application.

Can this be done?

Thanks.


回答1:


Look here Capturing console output from a .NET application (C#)




回答2:


This can be quite easily achieved using the ProcessStartInfo.RedirectStandardOutput Property. A full sample is contained in the linked MSDN documentation.

Process compiler = new Process();
compiler.StartInfo.FileName = "csc.exe";
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();    

Console.WriteLine(compiler.StandardOutput.ReadToEnd());

compiler.WaitForExit();


来源:https://stackoverflow.com/questions/6650086/getting-the-ouput-from-console-window-into-winform-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!