WMI namespace in .NET framework
There are two namespaces related to the WMI specification in the .NET framework, named System.Management and System.Management.InStrument two namespaces. The class object provided by System.Managemen namespace provides an object-oriented programming interface for accessing various management objects, and the class provided by the System.Management.instrumentation namespace is used to set the user-defined application to make these application objects. It is possible to comply with the WMI specification, so that through WMI to disclose management information and events provided by the manager of the application, these users may be like Microsoft Application Center or Microsoft Operations Manager, and it is possible to be a high-end-defined high end. Manage applications (you can write your own management application) via the class object provided by System.Management Namespaces. Therefore, if you want to make your own product to manage tools, you have to make your own application compliant with WMI specification, and write the application WMI specification on the .NET platform, use system . Management.instrumentation class provided by namespace.
System.Management namespace
The system.management namespace is WMI namespace in .NET Framework. This namespace includes the following important class objects:
ManagementClass: Management class, its constructor input parameter is the WMI class name, identify management object, for example:
Diskman = new managementClassClass ("Win32_LogicalDisk");
Diskman is the management object of the entire logical disk under the Win32 platform.
ManagementObjectCollection: ManagementObject The collection of objects.
ManagementObject and: ManagementObject For specific data management objects, for the above example, the C partition management object of the logical disk can be corresponding to the logical disk. Look at the following code:
*********************************************************** ***
ManagementObjectCollection Disks = Diskman.getInstances ();
Forum (ManagementObject Disk in Disks)
{
Console.writeline ("Disk = {0}", Disk ["" "]. TOSTRING ());
Console.writeline ("Disk = {0}", Disk ["FileSystem"]. TOSTRING ());
Console.Writeline ("Disk = {0}", Disk ["Size"]. TOSTRING ());
}
*********************************************************** ***
Disks is a collection of logical disk partitions C, D, E ... for each logical partition, manages its data by a Disk object, and outputs name, file system, and disk space.
ManagementQuery: Used as the basis for all query classes. As mentioned earlier, for managed objects, you can use SQL query statements in relational databases, such as:
Select * from win32_ logicaldiskManagementQuery As the abstract base class of the query, you can inherit to implement its method, SelectQuery is.
ManagementObjectSearcher: It is used to retrieve the collection of managed objects based on the specified query or enumerate.
*********************************************************** ***
SelectQuery SelectQuery = New SelectQuery ("Win32_LogicalDisk");
ManagementObjectSearcher Searcher =
New ManagementObjectSearcher (SelectQuery);
ForEach (ManagementObject Disk in search.get ())
{
Console.writeLine (Disk.toString ());
}
*********************************************************** **
The above code specifies the Searcher's query as SelectQuery, the collection query of the Win32 logical disk, and then outputs these logical disk partitions. Of course, you can also instantiate the managementObjectSearcher object directly using the SQL query statement. The code is as follows:
ManagementObjectSearcher search = new managementObjectsearchSearcher ("SELECT * WIN32_ LogicalDisk
");
ManagementEventWatcher: It is used to schedule WMI event notifications, which will be described in detail in later chapters.
Other classes of this namespace will be described in the following instance explanations.
System.Management.instrumentation namespace
From the previously described content, you should know if you want your own application or product to use management tools such as Microsoft Application Center or Microsoft Operations Manager, you should open your application object to support WMI tools. . Use will be the class provided by the System.Management.instrumentation namespace.
System.Management.instrumentation Namespaces allow you to complete the following tasks easier:
n standardized application;
n Exposes the application event as a WMI event;
n creation management object;
n Define and use the relationship between management objects.
Standardize the object of the application, so that it meets the WMI specification, such work is straightforward for .NET programmers. Because WMI architectural mode is object-oriented, there are many features and .NET metadata is connected. Therefore, the application's object can be mapped directly to WMI objects, the same, making application code transformation into manageable application code will not require a lot. For example, you can standardize the .NET application:
The N application components can occur.
n Provides manageable objects to make the application can be configured.
n Exposure time data, such as performance characteristics.
The following figure better illustrates the WMI normalization of the .NET application, and gives how to access management objects in the application in Windows and web-based programs.