How to obtain machine hardware information in. Net (Anti-software copy version)

Source: Internet
Author: User

Recently, I helped people develop something about software registration. The other party must prevent a large number of copies and prevent general use of registration codes. So I thought of using hardware information of machines to generate registration codes, so it is involved in obtaining machine hardware information...

In the. NET environment (described in VC #), you need to use a class library (system. Management. dll) to obtain the hardware information of the machine. In Solution Explorer, add system. Management.

We canProgramCompile it into a. dll file for future calls;

In the programCodeUsing system. Management;

The Code is as follows:

1. Obtain the machine Name:

Public String gethostname ()
{
Return System. net. DNS. gethostname ();
}

2. Obtain the CPU ID

Public String getcpuid ()

{

Managementclass MC = new managementclass ("win32_processor ");
Managementobjectcollection MOC = mc. getinstances ();

String strcpuid = NULL;
Foreach (managementobject Mo in MoC)
{
Strcpuid = Mo. properties ["processorid"]. value. tostring ();
Break;
}
Return strcpuid;

}

3. Obtain the hard drive number

Public String getmainharddiskid ()

{
String STR = "";
Managementclass mchd = new managementclass ("win32_logicaldisk ");
Managementobjectcollection mochd = mchd. getinstances ();
Foreach (managementobject m in mochd)
{
If (M ["DeviceID"]. tostring () = "C :")
{
STR = m ["volumeserialnumber"]. tostring ();
Break;
}
}
Return STR;
}

4. Obtain the bios and MAC address. This is complicated and requires netapi32.dll.
In addition:
5. Obtain the MAC address of the NIC
Using system. Management;
Public String getnetworkadapterid ()
{
String STR = "";
Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (bool) Mo ["ipenabled"] = true)
{
// Console. writeline ("MAC address \ t {0}", Mo ["macaddress"]. tostring ());
STR = Mo ["macaddress"]. tostring ();
Mo. Dispose ();
}
}
Return STR;
}
---------------------------------------------
If the system prompts that the namespace management cannot be found,
1. In Solution Explorer, right-click references and choose add reference...
2. The add reference dialog box is displayed, and the. NET tab is selected.
3. Select system. Management and click Select.
4. Click OK ".

Related Article

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.