C# 从其他控制台应用程序运行控制台应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2366168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run console application from other console app
提问by Dawid Ohia
I have a C# console application (A). I want to execute other console app (B) from within app A (in synchronous manner) in such way that B uses the same command window. When B exists, A should be able to read B's exit code.
我有一个 C# 控制台应用程序 (A)。我想从应用程序 A 中(以同步方式)以 B 使用相同命令窗口的方式执行其他控制台应用程序 (B)。当 B 存在时,A 应该能够读取 B 的退出代码。
How to do that? I need only this little tip on how to run this other app in same cmd window.
怎么做?我只需要关于如何在同一个 cmd 窗口中运行这个其他应用程序的这个小技巧。
采纳答案by Oded
You can use Process.Start
to start the other console application.
您可以使用Process.Start
来启动其他控制台应用程序。
You will need to construct the process with ProcessStartInfo.RedirectOutput
set to true
and UseShellExecute
set to false
in order to be able to utilize the output yourself.
您需要使用ProcessStartInfo.RedirectOutput
set totrue
和UseShellExecute
set to来构建流程,false
以便能够自己使用输出。
You can then read the output using StandardOutput.ReadToEnd
on the process.
然后,您可以StandardOutput.ReadToEnd
在进程上使用读取输出。
回答by Brian Rasmussen
You can start another process using the Process.Start()
call. The examples hereshow how to read output from other process and wait for it to finish.
回答by Paul Alexander
You can start another process with Process.Start - doesn't really matter if it's a console app or not. If your app is already running in a console window the newly spawned app will use that console window as well.
您可以使用 Process.Start 启动另一个进程 - 它是否是控制台应用程序并不重要。如果您的应用程序已经在控制台窗口中运行,则新生成的应用程序也将使用该控制台窗口。
var proc = Process.Start( "...path to second app" );
proc.WaitForExit();
var exitCode = proc.ExitCode;
Be sure to ready the docs on the Process class as there are a variety of little nuances that may affect the way your secondary app is launched.
请务必准备好 Process 类上的文档,因为有各种细微差别可能会影响您的辅助应用程序的启动方式。
回答by John Knoeller
Fill out a System.Diagnostics.ProcessStartInfoand pass it to Process.Start
填写System.Diagnostics.ProcessStartInfo并将其传递给Process.Start
You can WaitForExit
on the resulting process, and use then use ExitCode
property of the process to see the return value.
您可以WaitForExit
在生成的进程上,然后使用ExitCode
进程的use属性来查看返回值。
回答by sjors miltenburg
you can "wrap" the old console app with the new one by including it in your references and starting it off by calling whatever method is called in the run method of the program class
您可以通过将旧控制台应用程序包含在您的引用中并通过调用程序类的 run 方法中调用的任何方法来启动它,从而使用新的控制台应用程序“包装”旧的控制台应用程序
回答by Chesare
I was able to run the program 'B' as part of the same command window by calling the following configuration:
通过调用以下配置,我能够将程序“B”作为同一命令窗口的一部分运行:
ConsoleColor color = Console.ForegroundColor;
ProcessStartInfo startinfo = new ProcessStartInfo(nameProgramB);
startinfo.CreateNoWindow = false;
startinfo.UseShellExecute = false;
Process p = Process.Start(startinfo);
p.WaitForExit();
Console.ForegroundColor = color;
this way, both programs run seamlesly like they were one single program. 'nameProgramB' is the name to program 'B'. Hope this helps.
这样,两个程序无缝运行,就像它们是一个程序一样。'nameProgramB' 是编程 'B' 的名称。希望这可以帮助。