简体   繁体   中英

Getting Physical Disks serial number doesn't work on windows XP

i'm trying to get physical disks serial number using C# adn WMI

var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
foreach (ManagementObject mo in searcher.Get())
{
    Console.WriteLine("Serial: {0}", mo["SerialNumber"]);
}

this code show all serial numbers of HDD and flash removable stick, but it fails to get flash removable disk on windows XP and gives me null for the removable disks but get the HDD serial number correctly. any ideas what's wrong ?

my thinking is that windows XP needs update, anyone knows what is the update needed ? i need the specific update to include it with my software.

any other reasons why this happens ?

As far as I'm aware, it's not guaranteed that a storage device will report a serial number even if the code is otherwise correctly done. In the past I worked on some licensing code and ran into similar problems in various environments when using WMI. In my experience, it's a lot more reliable to use low-level API calls (but it's a lot more coding / testing) but even then, you're not guaranteed to get a serial number back.

Some environments where you may run into problems (not a complete list):

  • Windows XP
  • Virtual machines
  • SATA drives
  • Various other devices that present themselves as HDDs but really aren't
  • etc.

You can look at this article on Code Project and see if it works better for you.

If you're trying to do this for some kind of licensing solution, you may want to consider getting more hardware information so that if one piece of information is missing you still have something else to go with. It's also worth reading about spoofing HDD serial numbers .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM