WMI Code Creator for Bitlocker Status - where did

2019-08-03 06:54发布

enter image description hereI 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);
            }
        }
    }

enter image description here
(click for larger image)

1条回答
Anthone
2楼-- · 2019-08-03 07:19

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.

enter image description here


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.

查看更多
登录 后发表回答