C # obtain hardware information

Source: Internet
Author: User

Obtain the hard disk serial number
Methods circulating on the Internet:
Public String getharddiskid ()
{
Managementclass searcher = new managementclass ("win32_diskdrive ");
Managementobjectcollection MOC = searcher. getinstances ();
String strharddiskid = "";
Foreach (managementobject Mo in MoC)
{
Strharddiskid = Mo ["model"]. tostring (). Trim ();
Break;
}
Return strharddiskid;
}
Yes. The hard disk model is used in this method, not the serial number. The description on msdn: Manufacturer's model number of the disk drive.
Correct method:
Managementclass searcher = new managementclass ("win32_physicalmedia ");
Managementobjectcollection MOC = searcher. getinstances ();
// Hard disk serial number
String strharddiskid = "";
Foreach (managementobject Mo in MoC)
{
Try
{
Strharddiskid + = Mo ["serialnumber"]. tostring (). Trim ();
Break;
}
Catch
{
// It may be a USB flash disk or something like that.
}
}
Description of msdn: Manufacturer-allocated number used to identify the physical media. This is the identifier assigned by the manufacturer. That is to say, it is not specified that it must be unique, but only the identifier of the manufacturer, however, for a hard disk, it is unlikely that the serial number is repeated unless two hard disk manufacturers have identical naming rules,

Cpu id:
Methods circulating on the Internet:
Public String getcpuid ()
{
Managementclass searcher = new managementclass ("win32_processor ");
Managementobjectcollection MOC = searcher. getinstances ();
String strcpuid = "";
Foreach (managementobject Mo in MoC)
{
Strcpuid = Mo ["processorid"]. tostring (). Trim ();
Break;
}
Return strcpuid;
}
It is also wrong,
Description on msdn: processor information that describes the processor features. for an x86 class CPU, the Field Format depends on the processor support of the cpuid instruction. if the instruction is supported, the property contains 2 (two) DWORD formatted values. the first is an offset of 08h-0bh, which is the eax value that a cpuid instruction returns with input eax set to 1. the second is an offset of 0ch-0fh, which is the edX value that the instruction returns. only the first two bytes of the property are significant and contain the contents of the DX register at CPU reset-all others are set to 0 (zero), and the contents are in DWORD format.
It only describes a model number.
The correct one should be: win32_processor.uniqueid
Description on msdn: globally unique identifier for the processor. This identifier may only be unique within a processor family. This property is inherited from cim_processor.
This number is also unique for the manufacturer, but the manufacturer that produces the CPU ........., It should not be heavy.

Nic ID:
Public String getnetid ()
{
Managementclass searcher = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = searcher. getinstances ();
String strnetid = "";
Foreach (managementobject Mo in MoC)
{
If (Boolean) Mo ["ipenabled"])
{
Strnetid = Mo ["macaddress"]. tostring ();
Break;
}
}
Return strnetid;
}
The network card number is obtained from the MAC address, which is unique but can be modified by the user, and the machine may have multiple network cards, the NIC numbers obtained each time may be different (in the case of multiple NICs, you only need to disable the NIC and start it again, which may lead to different MAC addresses ),

There are two ways for attackers to spread on the Internet!

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/nuey1985/archive/2010/01/04/5127748.aspx

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.