使用ManagementObjectSearcher获取精确的BitLocker WMI值

时间:2017-03-23 18:22:38

标签: c# wmi bitlocker

美好的一天, 我遇到了ManagementObjectSearcher的问题。我试图查询我想要的确切值但无法找到任何对精确语法要求的引用,并且在尝试完成我需要的代码时不断收到错误。

提出问题的代码的特定部分是当我检查驱动器加密状态时(我知道我的磁盘未在此机器上加密的事实,这就是为什么这是我唯一的价值,如果' d目前)。任何有助于获取此代码以获取正确值的帮助将非常感激。

我尝试了“=”方法和“LIKE”方法,输出没有变化。

using Microsoft.Win32;
using System;
using System.Drawing;
using System.IO;
using System.Management;
using System.Windows.Forms;

    public Form1()
    {
        InitializeComponent();

        // Check for OS Version
        string OSVer = Convert.ToString(Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName", null));
        OSDialog.Text = OSVer;

        // Check Architecture
        if (Directory.Exists("C:\\Program Files (x86)"))
        {
            ArchitectureDialog.Text = "64 Bit";
        }
        else
        {
            ArchitectureDialog.Text = "32 Bit";
        }

        // Check Encryption
        ManagementObjectSearcher Collect = new ManagementObjectSearcher("SELECT ProtectionStatus FROM Win32_EncryptableVolume WHERE DriveLetter = 'C:'");

        string Encryption = Collect.ToString();

        if (Encryption == "0")
        {
            EncryptionDialog.Text = "Disk is not Encrypted";
            EncryptionDialog.ForeColor = Color.Green;
        }
    }

    private void Cancel_Click(object sender, EventArgs e)
    {
        Close();
    }

2 个答案:

答案 0 :(得分:0)

从WMI获取BitLocker信息需要提升权限。您的代码必须以管理员身份运行,您必须要求提升权限。因此,我不使用ManagementObjectSearcher来获取BitLocker信息。相反,我做了类似于以下的事情(根据您的情况进行了修改 - 但未按照显示进行测试):

ManagementObject GetBitLockerManager( string driveLetter )
{
    var path = new ManagementPath( );
    path.Server = string.Empty;
    path.NamespacePath = @"\ROOT\CIMV2\Security\MicrosoftVolumeEncryption";
    path.ClassName = "Win32_EncryptableVolume";

    var options = new ConnectionOptions( );
    options.Impersonation = ImpersonationLevel.Impersonate;
    options.EnablePrivileges = true;
    options.Authentication = AuthenticationLevel.PacketPrivacy;

    var scope = new ManagementScope( path, options );
    var mgmt = new ManagementClass( scope, path, new ObjectGetOptions( ) );

    mgmt.Get( );
    return mgmt
      .GetInstances( )
      .Cast<ManagementObject>( )
      .FirstOrDefault
      ( vol => 
        string.Compare
        (
          vol[ "DriveLetter" ] as string, 
          driveLetter, 
          true
        ) == 0 
      );
}

答案 1 :(得分:0)

好的,所以我想出来了,谢谢你提供的所有帮助。代码如下。

ManagementObjectSearcher Encryption = new ManagementObjectSearcher(@"root\cimv2\Security\MicrosoftVolumeEncryption", "SELECT * FROM Win32_EncryptableVolume");

        foreach (ManagementObject QueryObj in Encryption.Get())
        {
            string EncryptionStatus = QueryObj.GetPropertyValue("ProtectionStatus").ToString();

            if (EncryptionStatus == "0")
            {
                EncryptionDialog.Text = "Unencrypted";
            }
            else if (EncryptionStatus == "1")
            {
                EncryptionDialog.Text = "Encrypted - SysPrep will not complete";
            }
            else if (EncryptionStatus == "2")
            {
                EncryptionDialog.Text = "Cannot Determine Encryption";
            }
        }