Start a process in the same console

前端 未结 2 1147
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 04:46

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/er

2条回答
  •  有刺的猬
    2020-12-05 05:21

    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.

    I tried the following program:

    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:

    alt text

提交回复
热议问题