Hack 76. Enumerate Processes, Drives, Shares, and Moreyou can discover information about a system's drives, processes, printers, file shares, and much more. Install and use the WMI extensions for the Visual Studio Server Explorer.To access WMI from the Server Explorer, you will first need to download and install the WMI extensions for Server Explorer from http://msdn.microsoft.com/library/default.asp?url=/downloads/list/wmi.asp. Versions are currently available for Visual Studio .NET 2002 and Visual Studio .NET 2003.
nodes, Management Classes and Management Events, in the Server Explorer, as seen in Figure 9-24. Figure 9-24. Server ExplorerWMI nodes![]() 9.5.1. Adding ClassesYou can also see in Figure 9-24 a long list of the things you can interface with through WMI. You can add even more classes by right-clicking on the Management Classes node and choosing Add Classes. You will see the dialog shown in Figure 9-25. Figure 9-25. Add Classes dialog![]() you have to do is select the class from the Available Classes list and then click the Add button to move it to the Selected Classes side. When you click OK, the new class will be shown in the Server Explorer. 9.5.2. Creating Managed ClassesThe real benefit of having WMI classes in the Server Explorer is that you can then create strongly typed managed classes to interact with these WMI classes. Using WMI from .NET is not always easy or intuitive, but using a couple of functions available through the Server Explorer makes it much easier. To create a managed class for a WMI class, you simply need to right-click on the class and click Generate Managed Class, as shown in Figure 9-26. Figure 9-26. Choosing Generate Managed Class![]() your project. In the case of processes, the name of the class will be Win32_Process.CS. This file is a complete managed wrapper for the Process WMI class, including all methods, properties, and even custom collections.You can then use this class through your code to directly interface with WMI. Here is an example of getting a list of all the processes currently running on a machine: using System;The preceding code is a simple form that will loop through the process class and for each process add an item to the ListView. When this code is run, you will see a form like the one shown in Figure 9-27. Figure 9-27. List of processes currently running![]() processes in the ListView or let the user select a process and see more information about that process. Using the methods available on the Process class, you could also allow the user to set the priority of the process, set the owner, or terminate the process. Server Explorer. If you find yourself with the need to work with WMI, you should definitely make use of the WMI extensions. They make what would otherwise be a tedious task quite easy. |




