Hey all,
I'm trying to get info about installed software on local computers (one is Windows 7 and other XP SP3) and I'm able to do it with VBScript, but not with C#.
Here is the VBScript code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery _
("Select * from Win32_Product")
For Each objSoftware in colSoftware
Wscript.Echo "Name: " & objSoftware.Name
Wscript.Echo "Version: " & objSoftware.Version
Next
and here is the C# code:
string queryProd = "SELECT * FROM Win32_Product";
ObjectQuery oQuery = new ObjectQuery(queryProd);
ManagementObjectSearcher searcherProd = new ManagementObjectSearcher(oQuery);
ManagementObjectCollection resultCollection = searcherProd.Get();
foreach (ManagementObject prodVar in resultCollection)
{
Console.WriteLine("Product Name: {0}, Version: {1}.",
(prodVar["Name"] == null) ? prodVar["Name"] : "/",
(prodVar["Version"] == null) ? prodVar["Version"] : "/");
}
The second code snippet (C#) is not working. It doesn't give me any error, it just returns null. The thing is that C# code works flawlessly when I'm using some other WMI class, such as Win32_ComputerSystem, for example. But again, it's not working for Win32_DiskDrive class, also this particular case, etc.
In conclusion, in C#, WMI works only for some classes and in VBScript they all work. So, I'm wondering why is that?
Thanks for the answers.