Use of managementclass and managementobject

Source: Internet
Author: User

Original article: http://blog.csdn.net/hardstone1/article/details/5380775

OnlineCodeAnd msdn help do not list managementobject [""] which attributes can be used here? refer to examples ().

The useprop function describes how to obtain the IP address of the NIC to be activated and its driver.ProgramName. If you need other attributes of the network card, you will find the attributes in the getallprop () result. The attributes in English are not described in Chinese, too many, let's take a look at the text.

 

Public void getallprop () // obtain all attribute names and values of a managementclass [""]
{
Managementclass c = new managementclass ("win32_process ");
Managementclass MC;
MC = new managementclass ("win32_networkadapterconfiguration"); // Nic Information

// MC = new managementclass ("win32_process"); // view system process information

Managementobjectcollection MOC = mc. getinstances ();

Foreach (managementobject o in MoC)
Foreach (propertydata prop in O. properties)
Textbox2.text + = prop. Name + "---" + Prop. Value + "/R/N"; // list all available attribute names and values


}

 

Public String useprop () // use the specific attribute name
{
String [] Mac;
Managementclass MC;
MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (Mo ["ipenabled"]. tostring () = "true ")
{
MAC = (system. String []) Mo ["IPaddress"]; // obtain the IP address of a NIC. Multiple IP addresses are allowed. Therefore, the IP address type is string [].

Textbox2.text + = Mac [0]; // obtain the first IP address of a network card
Textbox2.text + = Mo ["servicename"] + "/R/N"; // The driver name of the NIC
}
}

Return "";
}

 

 

 

Original article: http://www.cnblogs.com/xiangdejun/archive/2010/07/22/1783061.html

1. Add

Reference of system. Management

2. initialize the reference in the CS code

Managementclass MC = new managementclass ("referenced WMI library name ");

The WMI class library that can be referenced can be found in msdn:

Http://msdn.microsoft.com/en-us/library/aa394084 (V = vs.85). aspx

3. Retrieve WMI instances

Managementobjectcollection MOC = vnetworkadapter. getinstances ();

What we get here isWMIInstance set. For example, you can obtain network adapter information. Here, you can obtain a set of instances related to network connections, such as wireless connections, wired connections, and 1394 connections.

4. Loop instances to obtain their attributes

Foreach (managementobject Mo in MoC)
{

// Todo: obtains the attributes of each instance.

// The attributes, methods, and explanations of the instance can also be found inHttp://msdn.microsoft.com/en-us/library/aa394084 (V = vs.85). aspxCheck

}

5. Modify

The managementobject. invokemethod (methodname, managementbaseobject, invokemethodoptions) method is required for modification.

MethodnameThe name of the method to be executed can also be found in the link of the above msdn;

Managementbaseobject is hard to explain. It is officially interpreted as "containing basic elements for object management. It is used as the base class for managing more specific management object classes ."

Invokemethodoptions execute the additional option, which can be null.

 

Complete instance:

Managementclass MC =   New Managementclass ( " Win32_networkadapterconfiguration " );
Managementobjectcollection MoC = MC. getinstances ();
Foreach (Managementobject Mo In MOC)
{
// Skip this step if no network device with IP settings Enabled
If ( ! ( Bool ) Mo [ " Ipenabled " ])
Continue ;

// Get attributes
String Caption = Mo [ " Caption " ]. Tostring (); // Name
String [] Addresses = ( String []) Mo [ " IPaddress " ]; // IP address
String [] Subnets = ( String []) Mo [ " Ipsubnet " ]; // Subnet Mask
String [] Gateways = ( String []) Mo [ " Defaultipgateway " ]; // Gateway
String [] Dnses = ( String []) Mo [ " Dnsserversearchorder " ]; // DNS

Managementbaseobject newip;

// Modify IP address and subnet mask
Newip = Mo. getmethodparameters ( " Enablestatic " );
Newip [ " IPaddress " ] =   New   String [] { " 192.168.2.51 " };
Newip [ " Subnetmask " ] =   New   String [] { " 255.255.255.0 " }; // When modifying the IP address, you must enter the subnet mask.
Mo. invokemethod ( " Enablestatic " , Newip, Null );

// Set gateway address
Newip = Mo. getmethodparameters ( " Setgateways " );
Newip [ " Defaultipgateway " ] =   New   String [] { " 192.168.2.1 " };
Mo. invokemethod ( " Setgateways " , Newip, Null );

// Set DNS
Newip = Mo. getmethodparameters ( " Setdnsserversearchorder " );
Newip [ " Dnsserversearchorder " ] =   New   String [] { " 192.168.1.10 " };
Mo. invokemethod ( " Setdnsserversearchorder " , Newip, Null );

}

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.