C # The content for WMI operations is encapsulated in system. Management. dll. The details are described in msdn. By default, ["** '] is used to read and set properties.
This section describes how to automatically generate a strongly typed packaging software, which is much easier to handle and does not need to repeatedly view parameters and other information.
1. Use wmiexplorer to generate the packaging class corresponding to WMI
2. Strong type operations can be performed on the generated class.
Wmiexplorer can be found at http://www.primaltools.com/downloads/communitytools.
As shown in, right-click the CIM category to be operated and select generate. The generated class includes various attributes and operation methods.
Take the process on the machine managed by win32_process as an example. The operation method for the generated class is as follows:
Using Root. cimv2.win32; process. staticscope = This . Scope; var PS = process. getinstances (); Foreach (VAR P In PS ){ Using (Process M = P As Process) {system. Diagnostics. Debug. writeline (M. Name ); If (M. Name =" Notepad.exe ") {M. Terminate (1 );}}} Uint PID; process. Create (" Notepad.exe ", Null , Null , Out PID); var my = process. getinstances ( This . Scope ," Processid = "+ PID. tostring ()); If (My. Count> 0) {var I = My. getenumerator (); I. movenext (); process P = I. CurrentAs Process; p. Terminate (1) ;}// comparison of another form:
Managementobject mo = New Managementclass ( This . Scope, New Managementpath (" Msft_sipesusercontactdata "), New Objectgetoptions (). createinstance (); Mo [" Userinstanceid "] = This . Userid; Mo [" Sipuri "] = Sip; Mo [" Groupid "] = Groupids; Mo [" Name "] = Name; Mo [" Subscribed "] = True ; Mo. Put (); sipesusercontactdata. staticscope = This . Scope; sipesusercontactdata c = sipesusercontactdata. createinstance (); C. lateboundobject [" Userinstanceid "] = This . Userid; C. lateboundobject [" Sipuri "] = Sip; C. lateboundobject [" Groupid "] = Groupids; C. lateboundobject [" Name "] = Name; C. lateboundobject [" Subscribed "] = True ; C. commitobject ();
As shown above, because the generated class contains the attribute name, you can directly refer to the class even when using the name.
It is much more convenient.
This example is an example of operations performed by the operating process and contacts on the office communicator server.
Http://cid-56b433ad3d1871e3.office.live.com/self.aspx/.Public/WMIDemo.zip