Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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.


advertisement
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.

using System; using System.Management; namespace ProcessFileListing { class ProcessFileListing { static void Main(string[] args) { ManagementClass processes = new ManagementClass("Win32_Process"); foreach(ManagementObject process in processes.GetInstances()) { Console.WriteLine(process["Caption"]); foreach(ManagementObject file in process.GetRelated("CIM_DataFile")) { Console.WriteLine("\t{0}", file["Caption"]); Console.WriteLine(); } } } } }

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Thanks for your registration, follow us on our social networks to keep up-to-date