Exploring WMI Part II: Using the Common Information Model : Page 4
Part II of this WMI tutorial series discusses details of the Common Information Model Version 2 (CIMV2) schema and shows you how to query CIM to obtain detailed information about CIM objects.
by Peter Koen
Feb 19, 2004
Page 4 of 5
How to Use CIM from .NET Code
The first article in this series provided some basic knowledge about the .NET classes in the System.Management namespace. This article doesn't focus on introducing new classes, but you'll see how to use a function called GetRelated() to explore associations in the CIM.
Build an Application to List Process Files
This short program demonstrates nicely how powerful WMI is. Consider a simple question you've probably asked yourself many times: "What files does this process execute?" WMI gives you a simple way to answer the question. The class Win32_Process has an association class called CIM_ProcessExecutable which links to a CIM_DataFile class. You simply follow the relationships to find the answer.
First, create a new console application and add a reference to the System.Management.dll assembly.
static void Main(string args)
ManagementClass processes = new
foreach(ManagementObject process in
foreach(ManagementObject file in
The code first creates a ManagementClass object for the Win32_Process CIM class. Then it calls GetInstances() to acquire all the running processes as a ManagementObjectCollection which it traverses with a simple foreach loop to output the process name on the screen. Next, it calls the GetRelated() method on each process to obtain any relations of type CIM_DataFile. Calling GetRelated() without specifying the type of the related class would return all relations; in this case that would include the user logon sessions and the computer system on which the process is running. The call to GetRelated() returns another ManagementObjectCollection. The code loops through that with foreach outputting the file name on the screen. Figure 5 shows some sample output from the application.