在同一控制台启动一个进程 [英] Start a process in the same console

查看:172
本文介绍了在同一控制台启动一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以启动一个进程(使用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 CreateProcess的函数是一个控制台应用程序将继承其父的控制台,除非你指定< A HREF =htt​​p://msdn.microsoft.com/en-us/library/ms684863.aspx> 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:\windows\system32\netstat.exe", "-n" ) 
        {
            UseShellExecute = false
        };

    p.Start();
    p.WaitForExit();

    Console.WriteLine( "World" );
    Console.ReadLine();
}

和它给了我这样的输出:

and it gave me this output:

这篇关于在同一控制台启动一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆