How to get unique hard drive serial number in C #

I am developing activation for the system. To generate the request code, I used the hard drive identifier, Bios identifier, and processor identifier. I used the following code to get the hard drive id.

private string getHardDiskID()
{
     string hddID = null;
     ManagementClass mc = new ManagementClass("Win32_LogicalDisk");
     ManagementObjectCollection moc = mc.GetInstances();
     foreach (ManagementObject strt in moc)
     {
         hddID += Convert.ToString(strt["VolumeSerialNumber"]);
     }
     return hddID.Trim().ToString();
}

But if I connect a removable drive, the value of this identifier will change. How to get UNIQUE serial number of hard drive ...? Thanks in advance.

+4
source share
3 answers

You can try the source :

As stated in the source, the best solution is to obtain the serial number of the hard drive provided by the manufacturer. This value will not change even if you format the hard drive.

 searcher = new
    ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

   int i = 0;
   foreach(ManagementObject wmi_HD in searcher.Get())
   {
    // get the hard drive from collection
    // using index
    HardDrive hd = (HardDrive)hdCollection[i];

    // get the hardware serial no.
    if (wmi_HD["SerialNumber"] == null)
     hd.SerialNo = "None";
    else
     hd.SerialNo = wmi_HD["SerialNumber"].ToString();

    ++i;
   }
+2

,

  ManagementObjectSearcher objSearcher = new
    ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

 objSearcher = new
    ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

   int i = 0;

   foreach(ManagementObject wmi_HD in objSearcher.Get())
   {

    // get the hard drive from collection
    // using index
    HardDrive hd = (HardDrive)hdCollection[i];

    // get the hardware serial no.
    if (wmi_HD["SerialNumber"] == null)

     hd.SerialNo = "None";

    else

     hd.SerialNo = wmi_HD["SerialNumber"].ToString();

    ++i;

   }

"wbemtest" Windows. WBEMTEST - , WQL.

0
ManagementObjectSearcher searcher;

searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
        string serial_number="";

        foreach (ManagementObject wmi_HD in searcher.Get())
        {

             serial_number = wmi_HD["SerialNumber"].ToString();


        }

        MessageBox.Show(serial_number);
0
source

Source: https://habr.com/ru/post/1584826/


All Articles