WMI Code Creator for Bitlocker Status - where did

2019-08-03 06:25发布

问题:

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);
            }
        }
    }


(click for larger image)

回答1:

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.

Namespace: System.Management
Assembly: System.Management (in System.Management.dll)

The first line tells you that you need to add using System.Management; or do System.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 for System.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 of System.Management.ManagementObject, replace your foreach with

foreach (System.Management.ManagementObject queryObj in searcher.Get())

to force it to use the correct class.