To obtain various configuration information about the running host, such as the nic mac address, IP address, hard disk partition, and hard disk number, you only need to reference system. Management. It is much simpler to implement than VC ++. For example, obtain the current enable Nic Mac and display it in the list box control:
Managementclass MC = new managementclass ("win32_networkadapterconfiguration"); <br/> managementobjectcollection MOC = MC. getinstances (); <br/> foreach (managementobject Mo in MoC) <br/>{< br/> If (bool) Mo ["ipenabled"] = true) <br/> listbox1.items. add (Mo ["macaddress"]. tostring (); </P> <p >}< br/>
The above code can only obtain the activated Nic, And the NIC must be connected. To obtain the MAC address even when the network connection is disconnected, the following code is required:
Managementclass MC = new managementclass ("win32_networkadapterconfiguration"); <br/> managementobjectcollection MOC = MC. getinstances (); <br/> foreach (managementobject Mo in MoC) <br/>{< br/> If (bool) Mo ["macaddress"]! = NULL) <br/> listbox1.items. Add (Mo ["macaddress"]. tostring (); </P> <p >}< br/>
The above code can get the disconnected Nic Mac, but cannot get the disabled Nic Mac.
In addition, the Code for obtaining the CPU serial number and hard disk serial number is as follows:
Private void getinfo () <br/>{< br/> string cpuinfo = ""; // CPU serial number <br/> managementclass cimobject = new managementclass ("win32_processor "); <br/> managementobjectcollection MOC = cimobject. getinstances (); <br/> foreach (managementobject Mo in MoC) <br/>{< br/> cpuinfo = Mo. properties ["processorid"]. value. tostring (); <br/> console. writeline ("CPU serial number:" + cpuinfo. tostring (); <br/>}< br/> // obtain the hard disk id <br/> string Hdid; <br/> managementclass cimobject1 = new managementclass ("win32_diskdrive"); <br/> managementobjectcollection moc1 = callback (); <br/> foreach (managementobject Mo in moc1) <br/>{< br/> Hdid = (string) Mo. properties ["model"]. value; <br/> console. writeline ("hard disk serial number:" + Hdid. tostring (); <br/>}< br/>