Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# 'Invalid class' in a simple WMI query

I will like to find some result after this query, but in the beginning of the foreach loop, the error "invalid class" occur.

string wmiQuery = string.Format("SELECT * FROM Win32_Process");
var searcher = new ManagementObjectSearcher(wmiQuery);
var wmiResults = searcher.Get();

foreach (ManagementObject retObject in wmiResults)
 {
   Console.WriteLine("[{0}]\tName: {1}", retObject["ProcessID"], retObject["Name"]);
 }

I use window 7 64, and i wonder if Win32_Process exists. I also use wmi code creator download it from http://www.microsoft.com/downloads/en/details.aspx?familyid=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en but i dont find any Win32_Process.

Somebody has an idea ?

like image 915
Guillaume V Avatar asked Aug 02 '11 20:08

Guillaume V


2 Answers

I solve my problem. It seem that my WMI was corrupt. After testing WMI with this step:

  1. Click Start, click Run, type wmimgmt.msc, and then click OK.
  2. Right-click WMI Control (Local), and then click Properties.

I saw Win32_Process was a invalid class I follow this step for repair my WMI, and it work

1) In the start menu type "cmd"

2) Type "net stop winmgmt" and press Enter

3) Open a Windows Explorer and locate the path to C:\ windows\system32\WBEM\ folder and rename the Repository folder to something else like RepositoryOLD (right click and choose 'Rename Folder').

4) restart the computer

5) In the start menu type "cmd"

6) Type "net stop winmgmt" and press enter

7) Type "winmgmt /resetRepository" and restart the computer.

like image 143
Guillaume V Avatar answered Sep 23 '22 13:09

Guillaume V


As dominus suggested, use the Process class:

...
Process[] processes = Process.GetProcesses();

foreach (Process process in processes)
    Console.WriteLine(process.ProcessName);
...
like image 42
Igor Turman Avatar answered Sep 21 '22 13:09

Igor Turman