ManagementObjectSearcher throws FileNotFoundException when iterating its objects in foreach loop

1.6k views Asked by At

I try to obtain some system information with several classes from System.Management namespace but any attempt to use ManagementObjectSearcher or ManagementObjectCollection collection items results in FileNotFoundException.

Below I present the problematic piece of code:

    public static string GetProcessorID() {
        var processorID = "";
        var query = "SELECT ProcessorId FROM Win32_Processor";
        var oManagementObjectSearcher = new ManagementObjectSearcher(query);

        foreach (var oManagementObject in oManagementObjectSearcher.Get()) {
            processorID = (string)oManagementObject["ProcessorId"];
            break;
        }

        return processorID;
    }

Exception is thrown in 'foreach' line, when trying to obtain next item from the collection.

It is observed on machine with windows xp professional sp3, with visual studio 2008 professional. I suppose that it can be something with my cpu, which is intel dual core - the same program on other machine with similar environment works perfect. The main difference between those machines is cpu.

Other parameters like MACAddress and SystemDrive provides the same problem, which suggests that it might be rather software issue (WMI?)

Very similar problem is described here -> http://news.softpedia.com/news/XP-SP3-Win32-Processor-Class-Labels-Intel-Core-2-Duo-CPUs-Incorectly-90201.shtml but the solution did not solved it.

Any ideas? Thanks in advance.

Dawid

2

There are 2 answers

0
P. Zantinge On

I know my answer is a bit late, but because it's the first hit on google when i searched my error i guess i put a link to the topic solving it for me.

System.Management.ManagementException: Not found

Basicly what you have to do is resolve your error's with WMI.

0
Maciej On

I have the same problem. And it don't crash on 'in word but on

oManagementObjectSearcher.Get().

Check the stack trace:

System.IO.FileNotFoundException - Nie można odnaleźć określonego modułu. (Wyjątek od HRESULT: 0x8007007E)
    Stack trace:
w System.Management.ThreadDispatch.Start()
   w System.Management.ManagementScope.Initialize()
   w System.Management.ManagementObjectSearcher.Initialize()
   w System.Management.ManagementObjectSearcher.Get()
[...]

When I moved to the My computer / right click / manage / services / WMI configration / right click / properties -> I got message that there is an error with message "Win32: Couldn't find module"