Skip to main content
Email Password   helpLost your password?
Screenshot - WIN32_Process.jpg

Introduction

This is the second article in the series of articles How To: (Almost) Everything In WMI via C#.

This article focuses on the Win32_Process class within the root\CIMV2 namespace. This library exposes all the properties and methods encapsulated in this namespace to your application (and there are quite a few). Using this library, you can enumerate properties of the processes on your machines, kick off processes, terminate processes, enumerate processes, and gather significant details regarding your processes. The library facilitates running any of these tasks on local or remote workstations.

Consideration

Please do not send me emails with instructions on how to perform these tasks using .NET native classes. That is not the point of these articles. I'm composing these articles for the purpose of demonstrating how to use WMI within C#.NET with the System.Management namespace. Also note that WMI is a bit slower than the .NET classes so if you have no specific need to use WMI, you should probably be using System.Diagnostics.Process instead.

Using the Attached Code

Methods (Local Machine or Remote Machine)

Instantiate the Local Provider

//using baileysoft.Wmi.Process; *must include
ProcessLocal processObject = new ProcessLocal(); 

Walkthrough All the Methods

 //Get Running Processes
 Console.WriteLine("Fetching Running Processes: ");
 foreach (string eachProcess in processObject.RunningProcesses())
 {
    Console.WriteLine("Process: " + eachProcess);
 }
 Console.WriteLine("");
 
 //Start Process
 string processName = "notepad.exe";
 Console.WriteLine("Creating Process: " + processName);
 Console.WriteLine(processObject.CreateProcess(processName));
 
 //Change the Priority
 Console.WriteLine("Setting Process Priority: Idle");
 processObject.SetPriority(processName, ProcessPriority.priority.IDLE);
 
 //Get the Owner of a Process
 Console.WriteLine("Process Owner: " + processObject.GetProcessOwner(processName));
 
 //Get the Process Owner's SID 
 Console.WriteLine("Process Owner SID: " + 
    processObject.GetProcessOwnerSID(processName));
 Console.WriteLine("");

 //Get a collection of all the Properties of a Process (Memory Usage, etc, etc)
 Console.WriteLine("Properties of Process: " + processName);
 foreach (string property in processObject.ProcessProperties(processName))
  {
      Console.WriteLine(property);
  }
  Console.WriteLine("");
  
  //Terminate a Process  
  Console.WriteLine("Killing Process: " + processName);
  processObject.TerminateProcess(processName);
  Console.WriteLine("Process Terminated");  
  
  Console.ReadLine();

Remote System Processes

In order to run the code above against a remote machine, you must instantiate the ProcessRemote class. During this instantiation, you need to either send in explicit credentials or you can send in null values if you're running this on a workstation on a domain, logged in with a domain account with the appropriate permissions to perform these tasks against the remote workstation.

Instantiating the Remote Provider

//using baileysoft.Wmi.Process; *must include
ProcessRemote processObject = 
              new ProcessRemote(userName,
                                password,
                                domain,
                                machine/ip);

Connecting to a Remote Machine Where You Want to Use The Domain Credentials from the LoggedIn User

ProcessRemote processObject = 
                    new ProcessRemote(null,
                                      null,
                                      null,
                                      machine/ip);

Using a Service Account to kick off a Remote Process

ProcessRemote processObject = 
                    new ProcessRemote("neal.bailey",
                                      "S3cr3tPa$$",
                                      "BAILEYSOFT",
                                      "192.168.2.1");
                                      
                    processObject.OneOfTheMethodsDetailedAbove;

Conclusion

The WMI (Windows Management Instrumentation) provider is considerably slower than the native .NET classes. At first it may seem pointless to use WMI for process management tasks considering the ease of use of the .NET System.Diagnostics.Process classes, however a lot of developers out there spent a lot of time learning WMI and would like to have it available in their toolbox.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
General.NET C# start remote process and send commands(strings) to it Pin
legeti
12:28 10 Nov '09  
GeneralTrying to use UNC with this code snippet Pin
thrashmagazine
9:54 13 Apr '09  
GeneralRe: Trying to use UNC with this code snippet Pin
thund3rstruck
10:14 13 Apr '09  
GeneralRe: Trying to use UNC with this code snippet Pin
thrashmagazine
4:31 14 Apr '09  
GeneralRe: Trying to use UNC with this code snippet Pin
thund3rstruck
5:02 14 Apr '09  
QuestionE_ACCESSDENIED Pin
Nchek2000
5:57 28 Nov '07  
GeneralPrinting Pin
paulsoren
9:00 3 Apr '07  
GeneralRe: Printing Pin
thund3rstruck
13:30 3 Apr '07  
GeneralWMI Pin
Jamsey12001
4:11 3 Apr '07  
GeneralRe: WMI Pin
thund3rstruck
5:27 3 Apr '07  
GeneralRe: WMI Pin
Jamsey12001
5:39 3 Apr '07  
GeneralRe: WMI Pin
thund3rstruck
13:28 3 Apr '07  
GeneralRe: WMI Pin
tag2001
18:34 22 Oct '07  
GeneralRe: WMI Pin
thund3rstruck
4:06 23 Oct '07  
GeneralRe: WMI Pin
andyzheng
23:02 30 Oct '08  
GeneralRe: WMI Pin
thund3rstruck
5:18 31 Oct '08  
GeneralRe: WMI Pin
preusst
4:00 25 Jun '09  
GeneralRe: WMI Pin
thund3rstruck
4:34 25 Jun '09  
GeneralThank you Pin
Morten B
19:18 2 Apr '07  


Last Updated 19 Oct 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009