3

I'm using this code to fetch the processor id:

    public static string getProcessorId()
    {
        var mc = new ManagementClass("Win32_Processor");
        var moc = mc.GetInstances();

        foreach (var mo in moc)
        {
            return mo.Properties["ProcessorId"].Value.ToString();
        }

        return "Unknown";
    }

I'm running Windows 7 32-bit, Visual Studio 2008. Unfortunately, a "Not found" exception is being raised by the mc.GetInstances() method call.

Here's a similar bit of code (fetch HDD serial):

    public static string getVolumeSerialNumber()
    {
        var disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
        disk.Get();
        return disk["VolumeSerialNumber"].ToString();
    }

This code also fails - the "disk.Get()" method raises an "Invalid class" exception.

I've run this code with UAC turned off & on - nothing helps.

What am I doing wrong?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
invarbrass
  • 2,023
  • 4
  • 20
  • 23

1 Answers1

4

You WMI installation seems somewhat broken, I have tested your getProcessorId code on a Windows 7 with UAC on, and it works fine. "Win32_Processor" is a really standard class that should be there.

Here is a link to help diagnose WMI issues: How to check the WMI repository before rebuilding it

Matt
  • 74,352
  • 26
  • 153
  • 180
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • 2
    Thanks! It works now.. I had to reset the WMI repository via "winmgmt /resetrepository" command. – invarbrass Dec 26 '10 at 14:28
  • saved my life! although the link is broken, there's an updated link from microsoft https://techcommunity.microsoft.com/t5/ask-the-performance-team/wmi-rebuilding-the-wmi-repository/ba-p/373846 – Dany Balian Jul 26 '23 at 21:52