问题描述
我可以在与调用程序相同的控制台中启动进程吗(使用 C# Process.Start()
)?这样就不会创建新窗口,标准输入/输出/错误将与调用控制台应用程序相同.我尝试设置 process.StartInfo.CreateNoWindow = true;
但进程仍然在新窗口中启动(并在完成后立即关闭).
Can I start a process (using C# Process.Start()
) in the same console as the calling program? This way no new window will be created and standard input/output/error will be the same as the calling console application. I tried setting process.StartInfo.CreateNoWindow = true;
but the process still starts in a new window (and immediately closes after it finishes).
推荐答案
除了将 UseShellExecute = false
设置为 Win32 函数用于控制台应用程序继承其父级的控制台,除非您指定 CREATE_NEW_CONSOLE 标志.
You shouldn't need to do anything other than set UseShellExecute = false
, as the default behaviour for the Win32 CreateProcess function is for a console application to inherit its parent's console, unless you specify the CREATE_NEW_CONSOLE flag.
我尝试了以下程序:
private static void Main()
{
Console.WriteLine( "Hello" );
var p = new Process();
p.StartInfo = new ProcessStartInfo( @"c:windowssystem32
etstat.exe", "-n" )
{
UseShellExecute = false
};
p.Start();
p.WaitForExit();
Console.WriteLine( "World" );
Console.ReadLine();
}
它给了我这个输出:
这篇关于在同一个控制台中启动一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!