I am working with the WMI Code Creator and the code looks to work properly from the app. HOWEVER, It comes up with errors internal of my code that I cant seem to shake. Am I supposed to have a reference for this to work? if so where can I get it?
public class MyWMIQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2\\Security\\MicrosoftVolumeEncryption",
"SELECT * FROM Win32_EncryptableVolume");
foreach (System.Management.ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_EncryptableVolume instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("ProtectionStatus: {0}", queryObj["ProtectionStatus"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
}
}
If you search the MSDN for
ManagementObjectSearcher
you get this page. On every MSDN page for a .NET class you will see two pieces of information at the top of the page.The first line tells you that you need to add
using System.Management;
or doSystem.Management.ManagementObjectSearcher
if you want to reference the object.The second line tells you your project must reference the file
System.Management.dll
for your code to be able to find the class. If you search in the Add Reference Dialog you need to look forSystem.Management
(the part outside of the parenthesis) in the list.The second part of your problem is you have a class called
ComplianceGuide.ManagmentObject
in your project and Visual Studio is picking that reference up instead ofSystem.Management.ManagementObject
, replace your foreach withto force it to use the correct class.