0
public static string? GetDriveSerialNumber()
    {
        var moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
        
        foreach (var o in moSearcher.Get())
        {
            var wmiHd = (ManagementObject)o;
            if(wmiHd==null)
            {
                continue;
            }

            if (wmiHd["SerialNumber"]==null)
            {
                myApi.LoggerOut(LogCode.LogWarn, "serial number is null");
                continue;
            }
            var serialNumber = wmiHd["SerialNumber"].ToString();
            
            if (string.IsNullOrEmpty(serialNumber))
            {
                continue;
            }
            return serialNumber;
        }
        return null;
    }

In some machines it doesn't work. It looks like it returns null for virtual disks. Is it true? Any workaround?

Tried to install it in several servers with Windows Server 2019 x64. Worked fine in computers with physical storage. Didn't work with virtual disks.

Alenern
  • 9
  • 2
  • 3
    Virtual disks don't have serial numbers. Only physical disks do. Why would a virtual disk have a serial number? – Ken White Apr 15 '23 at 22:20
  • Have no idea.. Finally under any virtual disk there should be a physical drive, right? https://stackoverflow.com/questions/4084402/get-hard-disk-serial-number The approach described here will work? A combination of model, manufacturer, signature and total heads will always be unique? – Alenern Apr 15 '23 at 22:53
  • 2
    The virtual disk can be backed by a physical disk, and that **physical disk** would have a serial number. The virtual disk does not, and there would be no purpose in it having one. The post office wouldn't assign a separate address to the tent you put up in your back yard for your children to sleep in during the summer, would they? – Ken White Apr 15 '23 at 22:56
  • @KenWhite I'm pretty sure the children would love it. Sent them a letter when they are camping in the back yard. – rene Apr 16 '23 at 08:14
  • What about signature? Why it can't pull signature value for vitual disk? var signature = wmiHd["Signature"].ToString(); – Alenern Apr 22 '23 at 20:47

0 Answers0