none
ManagementClass("Win32_DiskDrive") not working in some system RRS feed

  • Question

  • I am using .Net 2017, I was trying to retrieve hard disk info such as SerialNumber and SerialNumber of Physical Memory etc. 

    The issue is that, the code is working fine in one of my PC and its not working in another PC. This is the code I have written.

                ManagementClass mc_2 = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc_2 = mc_2.GetInstances();
                foreach (ManagementObject mo in moc_2)
                {
                    try
                    {
                        Obj_Hardware_Info.Str_HDD_Serial_Number = mo.Properties["SerialNumber"].Value.ToString();
                        mo.Dispose();
                        break;
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                        throw;
                    }
                }
    


    I am getting 

    - $exception {"Object reference not set to an instance of an object."} System.NullReferenceException

    Thanks in advance.

    Manu.

    Sunday, July 14, 2019 3:15 PM

Answers

  • Maybe the device does not provide a serial number.

    You can check it using the “wbemtest” system tool, from Run menu. Use the “Connect” button with default settings. Press “Enum Instances”, paste “Win32_DiskDrive”. Double click the results to see the Serial Numbers.

     

    Monday, July 15, 2019 5:37 AM

All replies

  • {"Object reference not set to an instance of an object."}
    System.NullReferenceException

    It means that an object the code is trying to access is a null object. There is no instance of the object in memory. 

    It's OO 101 and it works the same on any OO language platform such as Java or .NET.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Use the Visual Studio debugger  Quickwatch and find out what is a null valued object.

    Just becuase it works on one machine's internals, brand name and a given O/S does it mean it will be the same on another machine that has differences, apparently. 

    Maybe, there is a workaround.

    Monday, July 15, 2019 12:31 AM
  • Maybe the device does not provide a serial number.

    You can check it using the “wbemtest” system tool, from Run menu. Use the “Connect” button with default settings. Press “Enum Instances”, paste “Win32_DiskDrive”. Double click the results to see the Serial Numbers.

     

    Monday, July 15, 2019 5:37 AM
  • Thank you. When I checked using the mentioned method, the value is null.

    Manu.

    Monday, July 15, 2019 1:57 PM