开发者

"Not found" exception generated while trying to get CPU ID via WMI

开发者 https://www.devze.com 2023-02-01 19:29 出处:网络
I\'m using this code to fetch the processor id: public static string getProcessorId() { var mc = new ManagementClass(\"Win32_Processor\");

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 W开发者_运维百科indows 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号