0

Most desktops run this perfectly, but when it runs on a newly configured desktop like 7th generation clone pc, the whole application closes with an error stating it can not read HDD. But why? I use .net 4.5.

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

foreach (ManagementObject wmi_HD in searcher.Get())
{
   string SerialNo = wmi_HD.GetPropertyValue("Signature").ToString();

   if (SerialNo == "0" || SerialNo == "" || SerialNo == null)
   {
   }
   else
   {
      hdCollection.Add(SerialNo);
   }
}
Jony Adamit
  • 3,178
  • 35
  • 44
mujibsani
  • 1
  • 2
  • 3
    Please provide the exact error you receive. – Jony Adamit Jan 28 '18 at 09:05
  • Please check this answer: https://stackoverflow.com/questions/4084402/get-hard-disk-serial-number. It is quite old, but the answer is saying that serial number comes from Win32_PhysicalMedia, not Win32_DiskDrive. – felbus Jan 28 '18 at 09:41
  • There hasn't any error. if i used these line of codes in my project. it works in every desktop without which desktops are built with new configuration. my team tested almost 5k+ desktop . 4th,5th,6th generation based desktop it runs perfectly. but when try to run in new 7th gen configured with Windows 10. that's time automatically apps has been closed. this is the problem. – mujibsani Jan 28 '18 at 09:46
  • Did you try what @felbus suggested? – Jony Adamit Feb 04 '18 at 08:23
  • I am trying to get signature not serial number. signature number only get from Win32_DiskDrive. @JonyAdamit – mujibsani Feb 05 '18 at 04:12

1 Answers1

1

You could write a fallback mechanism, to retrieve the signature from the registry.
It is located under HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices. First 4 bytes in the Data values are the signature.

Jony Adamit
  • 3,178
  • 35
  • 44