Posts tagged ‘System.Diagnostics’

C#: Launch Hidden Commandline Process and Retrieve its Output

Sometimes there may be a need to launch a command line operation which calls either an executable or a windows command and make it hidden. Within that operation data will be returned and the main code can process it. Here is a quick example to do that where a call to IPConfig is made. By simply setting the window style to hidden one can execute something without a window flashing at the user.

ProcessStartInfo psInfo = new System.Diagnostics.ProcessStartInfo(@"ipconfig");

psInfo.WindowStyle            = System.Diagnostics.ProcessWindowStyle.Hidden;
psInfo.RedirectStandardOutput = true;
psInfo.UseShellExecute        = false;

Process GetIPInfo;
GetIPInfo = Process.Start( psInfo );

// We want to get the output from standard output
System.IO.StreamReader myOutput = GetIPInfo.StandardOutput;
GetIPInfo.WaitForExit( 3000 );

if ( GetIPInfo.HasExited )

Note This requires System.Diagnostics namespace to be included.