Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start a process in the same console

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).

like image 336
Louis Rhys Avatar asked Sep 03 '10 07:09

Louis Rhys


People also ask

What does process Start do?

Start(ProcessStartInfo)Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component.

How do I start a process in VB net?

Start another application using your . NET code As a . NET method, Start has a series of overloads, which are different sets of parameters that determine exactly what the method does. The overloads let you specify just about any set of parameters that you might want to pass to another process when it starts.


1 Answers

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

like image 170
Phil Devaney Avatar answered Sep 30 '22 00:09

Phil Devaney