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


Leave a Reply