I'm using the following code to get my drive serial number. It's working fine with Windows 7, 8, 8.1, and 10 Professional, but I'm getting an error on Windows 10 Home.

```
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject wmi_HD in searcher.Get())
{
     if (wmi_HD["SerialNumber"] == null)
          hddId = null;
     else
          hddId = wmi_HD["SerialNumber"].ToString();
}

```

I'm getting

> 
> System.NullReferenceException : Object reference not set to an instance of an object.
> 
> 
> 

Does anyone know why? What do I need to do to get the serial number in this case?

One more question: if I boot the OS from my pendrive, will this code work? How could I know that the OS is running from a pendrive or disk or any other resource?

When I go to the Device Manager, I see this:

[![enter image description here](https://i.stack.imgur.com/glSHI.png)](https://i.stack.imgur.com/glSHI.png)