Click here to Skip to main content
15,889,116 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
4.50/5 (23 votes)
19 Oct 2007CPOL2 min read 317.2K   7.8K   115   43
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

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

Walkthrough All the Methods

C#
//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

C#
//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

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

Using a Service Account to kick off a Remote Process

C#
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)


Written By
Software Developer
United States United States
I'm a professional .NET software developer and proud military veteran. I've been in the software business for 20+ years now and if there's one lesson I have learned over the years, its that in this industry you have to be prepared to be humbled from time to time and never stop learning!

Comments and Discussions

 
QuestionUpdate Remote process owner Pin
Ajay K.N13-Apr-16 2:51
Ajay K.N13-Apr-16 2:51 
QuestionCreating a virtual network adapter in C # Pin
209723-Dec-14 18:09
209723-Dec-14 18:09 
QuestionMultiple processes on a server Pin
immrtimN29-Apr-14 21:55
immrtimN29-Apr-14 21:55 
QuestionBinary values not returned Pin
sigma_ivan22-Dec-13 22:35
sigma_ivan22-Dec-13 22:35 
GeneralMy vote of 5 Pin
homestar8519-Mar-13 0:25
homestar8519-Mar-13 0:25 
QuestionTRYING TO START NOTEPAD ON REMOTE MACHINE USING WMI Pin
CJayMeister12-Jun-12 21:10
CJayMeister12-Jun-12 21:10 
AnswerRe: TRYING TO START NOTEPAD ON REMOTE MACHINE USING WMI Pin
thund3rstruck13-Jun-12 1:39
thund3rstruck13-Jun-12 1:39 
GeneralMy vote of 3 Pin
Murhaf Suz12-May-12 23:49
Murhaf Suz12-May-12 23:49 
GeneralRe: My vote of 3 Pin
thund3rstruck13-Jun-12 1:41
thund3rstruck13-Jun-12 1:41 
GeneralMy vote of 4 Pin
kiquenet.com11-Jan-11 7:02
professionalkiquenet.com11-Jan-11 7:02 
Generalremote process start problem Pin
iro4es23-Feb-10 6:26
iro4es23-Feb-10 6:26 
GeneralRe: remote process start problem Pin
Boštjan Primožič12-Oct-10 10:32
Boštjan Primožič12-Oct-10 10:32 
GeneralRe: remote process start problem [modified] Pin
thund3rstruck12-Oct-10 10:55
thund3rstruck12-Oct-10 10:55 
Any solution for what?

Works perfectly fine here. I just re-ran the samples against several 2003 servers from XP and Vista workstations and they all ran flawlessly.

Creating Process: notepad.exe
Successful(Completion)
Setting Process Priority: Idle
Process Owner: developer
Process Owner SID: S-1-5-21-3087281821-956584911-1637330626-1118
Properties of Process: notepad.exe
Caption: notepad.exe
CommandLine: notepad.exe
CreationClassName: Win32_Process
CreationDate: 20101012170109.859190-240
CSCreationClassName: Win32_ComputerSystem
CSName: BAILEYWEB01
Description: notepad.exe
ExecutablePath: C:\WINDOWS\system32\notepad.exe
Handle: 3024
HandleCount: 29
KernelModeTime: 1093750
MaximumWorkingSetSize: 1380
Memory Usage: 2204
MinimumWorkingSetSize: 200
Name: notepad.exe
OSCreationClassName: Win32_OperatingSystem
OSName: Microsoft Windows Server 2003 R2 Enterprise Edition|C:\WINDOWS|\Device\Harddisk0\Partition1
OtherOperationCount: 62
OtherTransferCount: 40
PageFaults: 632
PageFileUsage: 728
ParentProcessId: 3072
PeakPageFileUsage: 788
PeakVirtualSize: 29417472
PeakWorkingSetSize: 2204
Priority: 4
PrivatePageCount: 745472
ProcessId: 3024
QuotaNonPagedPoolUsage: 2
QuotaPagedPoolUsage: 43
QuotaPeakNonPagedPoolUsage: 3
QuotaPeakPagedPoolUsage: 58
ReadOperationCount: 0
ReadTransferCount: 0
SessionId: 0
ThreadCount: 1
UserModeTime: 156250
VirtualSize: 22614016
WindowsVersion: 5.2.3790
WorkingSetSize: 2256896
WriteOperationCount: 0
WriteTransferCount: 0

Killing Process: notepad.exe
Process Terminated


modified on Tuesday, October 12, 2010 5:01 PM

GeneralRe: remote process start problem Pin
Boštjan Primožič13-Oct-10 5:32
Boštjan Primožič13-Oct-10 5:32 
GeneralRe: remote process start problem Pin
thund3rstruck13-Oct-10 9:04
thund3rstruck13-Oct-10 9:04 
GeneralRe: remote process start problem Pin
Boštjan Primožič13-Oct-10 10:11
Boštjan Primožič13-Oct-10 10:11 
GeneralRe: remote process start problem Pin
Boštjan Primožič19-Oct-10 0:43
Boštjan Primožič19-Oct-10 0:43 
GeneralRe: remote process start problem Pin
master_ua3-Dec-10 2:03
master_ua3-Dec-10 2:03 
General.NET C# start remote process and send commands(strings) to it Pin
legeti10-Nov-09 11:28
legeti10-Nov-09 11:28 
GeneralTrying to use UNC with this code snippet Pin
thrashmagazine13-Apr-09 8:54
thrashmagazine13-Apr-09 8:54 
GeneralRe: Trying to use UNC with this code snippet Pin
thund3rstruck13-Apr-09 9:14
thund3rstruck13-Apr-09 9:14 
GeneralRe: Trying to use UNC with this code snippet Pin
thrashmagazine14-Apr-09 3:31
thrashmagazine14-Apr-09 3:31 
GeneralRe: Trying to use UNC with this code snippet Pin
thund3rstruck14-Apr-09 4:02
thund3rstruck14-Apr-09 4:02 
QuestionE_ACCESSDENIED Pin
Nchek200028-Nov-07 4:57
Nchek200028-Nov-07 4:57 
AnswerRe: E_ACCESSDENIED Pin
thund3rstruck12-Oct-10 11:05
thund3rstruck12-Oct-10 11:05 

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

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