WMI in C # [Strongly typed operations]

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.