Click here to Skip to main content
Click here to Skip to main content

How To: (Almost) Everything In WMI via C# Part 2: Processes

, 19 Oct 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
A C# Wrapper for WMI Win32_Process Class
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)

  • CreateProcess(string processPath) - Starts a process
  • GetProcessOwner(string processName) - Gets the user name of the process owner
  • GetProcessOwnerSID(string processName) - Gets the SID of the process owner
  • ProcessProperties(string processName) - Gets the 60+ property values of the process
  • RunningProcesses() - Gets the names of all the running processes (can be changed)
  • SetPriority(string processName, ProcessPriority.priority priority) - Changes the process priority
  • TerminateProcess(string processName) - Kills the process

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

  • Originally submitted on 26th March, 2007

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

thund3rstruck
Software Developer
United States United States
I'm a typical 30 year old generation X guy that likes video games, NFL football, and comic style art. I have an insatiable passion for programming and doing what ever it takes to become a better programmer.

Comments and Discussions

 
QuestionMultiple processes on a server [modified] PinmemberMember 1070078129-Apr-14 22:55 
QuestionBinary values not returned Pinmembersigma_ivan22-Dec-13 23:35 
GeneralMy vote of 5 Pinmemberhomestar8519-Mar-13 1:25 
QuestionTRYING TO START NOTEPAD ON REMOTE MACHINE USING WMI PinmemberCJayMeister12-Jun-12 22:10 
AnswerRe: TRYING TO START NOTEPAD ON REMOTE MACHINE USING WMI Pinmemberthund3rstruck13-Jun-12 2:39 
GeneralMy vote of 3 Pinmemberbboy_markor13-May-12 0:49 
GeneralRe: My vote of 3 Pinmemberthund3rstruck13-Jun-12 2:41 
GeneralMy vote of 4 Pinmemberalhambra-eidos11-Jan-11 8:02 
Generalremote process start problem Pinmemberiro4es23-Feb-10 7:26 
GeneralRe: remote process start problem PinmemberBoštjan Primožič12-Oct-10 11:32 
GeneralRe: remote process start problem [modified] Pinmemberthund3rstruck12-Oct-10 11:55 
GeneralRe: remote process start problem PinmemberBoštjan Primožič13-Oct-10 6:32 
GeneralRe: remote process start problem Pinmemberthund3rstruck13-Oct-10 10:04 
GeneralRe: remote process start problem PinmemberBoštjan Primožič13-Oct-10 11:11 
GeneralRe: remote process start problem PinmemberBoštjan Primožič19-Oct-10 1:43 
GeneralRe: remote process start problem Pinmembermaster_ua3-Dec-10 3:03 
General.NET C# start remote process and send commands(strings) to it Pinmemberlegeti10-Nov-09 12:28 
GeneralTrying to use UNC with this code snippet Pinmemberthrashmagazine13-Apr-09 9:54 
GeneralRe: Trying to use UNC with this code snippet Pinmemberthund3rstruck13-Apr-09 10:14 
GeneralRe: Trying to use UNC with this code snippet Pinmemberthrashmagazine14-Apr-09 4:31 
GeneralRe: Trying to use UNC with this code snippet Pinmemberthund3rstruck14-Apr-09 5:02 
QuestionE_ACCESSDENIED PinmemberNchek200028-Nov-07 5:57 
AnswerRe: E_ACCESSDENIED Pinmemberthund3rstruck12-Oct-10 12:05 
GeneralPrinting Pinmemberpaulsoren3-Apr-07 9:00 
GeneralRe: Printing Pinmemberthund3rstruck3-Apr-07 13:30 
GeneralWMI PinmemberJamsey120013-Apr-07 4:11 
GeneralRe: WMI Pinmemberthund3rstruck3-Apr-07 5:27 
GeneralRe: WMI PinmemberJamsey120013-Apr-07 5:39 
GeneralRe: WMI Pinmemberthund3rstruck3-Apr-07 13:28 
GeneralRe: WMI Pinmembertag200122-Oct-07 18:34 
GeneralRe: WMI Pinmemberthund3rstruck23-Oct-07 4:06 
GeneralRe: WMI Pinmemberandyzheng30-Oct-08 23:02 
GeneralRe: WMI Pinmemberthund3rstruck31-Oct-08 5:18 
GeneralRe: WMI Pinmemberpreusst25-Jun-09 4:00 
GeneralRe: WMI Pinmemberthund3rstruck25-Jun-09 4:34 
GeneralRe: WMI Pinmemberalhambra-eidos11-Jan-11 8:02 
GeneralRe: WMI Pinmemberthund3rstruck11-Jan-11 8:30 
GeneralRe: WMI Pinmemberibhatti8119-Oct-10 11:38 
GeneralRe: WMI Pinmemberalhambra-eidos11-Jan-11 8:01 
GeneralRe: WMI Pinmemberthund3rstruck11-Jan-11 8:23 
GeneralThank you PinmemberMorten B2-Apr-07 19:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 19 Oct 2007
Article Copyright 2007 by thund3rstruck
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid