在c#中执行控制台应用程序?

001*_*001 4 c# console class-library class console-application

可能重复:
如何在隐藏控制台的情况下运行C#控制台应用程序.

通过类库如何在后台运行控制台应用程序来执行任务,然后告诉我的方法完成处理?

She*_*Pro 5

您可以使用Process类.

这是一个例子:

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "your application path";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

还有一个HasExited属性来检查进程是否已完成.

你可以像这样使用它:

if (p.HasExited)...
Run Code Online (Sandbox Code Playgroud)

或者您可以将EventHandler绑定到Exited事件.