Posts Tagged Process

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.


Tags: ,

C# WMI Get Process Start Location

This question was asked in the forums, the user had two different autocads running. Each ran from a different location and the user needed to programmatically know where it was running from. The following code snippet provides that information to the user on all processes.

// Add System.Management reference to the project
using System.Management;
using System.Management.Instrumentation;

ManagementClass mc = new ManagementClass( "Win32_Process" );

ManagementObjectCollection mcol = mc.GetInstances();

foreach ( ManagementObject strt in mcol )
   Console.WriteLine( string.Format("\t-{0}-", strt["Name"]));
   Console.WriteLine( "Path:\t" + strt["ExecutablePath"] );
   Console.WriteLine( Environment.NewLine );

Here is an example output on my box while running Windows Live Writer:


Path:   C:\Program Files\Windows Live\Writer\WindowsLiveWriter.exe


Tags: , ,

How to Launch an External Application in C#

Here is a C# example where one can execute a program using the Process Class. The example launches notepad with an argument of a filename to open and waits for completion. When the user closes notepad, its return code is printed out.


Process pr = new Process();

pr.StartInfo.FileName = "Notepad.exe";
pr.StartInfo.Arguments = "test.dat";

while (pr.HasExited == false)
    if ((DateTime.Now.Second % 5) == 0)
    { // Show a tick every five seconds.

One can extract any output that a program does to the command line by looking at the Process.StandardOutput Property if it is applicable.



Launch Browser from code with Generated HTML in C#

There is a quick way to have a companion browser come up with generated html from your application in C# and .Net. The following example simulates getting or generating the html, saving it to a file and then launching the file from the command line.

First off you will need to include these namespaces:
using System.Diagnostics;
using System.IO;
Then you can do this code.
string filename
    = string.Format(@"{0}\{1}",

File.WriteAllText(filename, "<h1>Hello</h1>");

  • Line 02: We assume the user does not have the final slash. A more diligent programmer will check for the slash.
  • Line 06: We simply write out the html to a file.
  • Line 08: Thanks to the OS it has assigned the extension to a launchable application and htm usually goes with the default browser. By calling the static Start on the Process Class we launch the html file.
Don’t forget to delete (or attempt to delete the file) when your program closes as a courtesy.