1

Good Day All, I am having an issue with ManagementObjectSearcher. I am trying to query the exact value that i want but cannot find any reference to the precise syntax requirements and I continually receive an error when trying to finish out the code to be what I need it to be.

the specific portion of code that is presenting the issue is when I check for the drives Encryption state(I know for a fact that my disk is not encrypted on this machine, which is why that is the only value i have if'd currently). Any assistance in getting this code to pull the correct value would be greatly appreciated.

I've tried both the "=" method and the "LIKE" method with no change in output.

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

2 回答 2

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";
            }
        }
于 2017-03-29T13:51:22.463 回答
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 
      );
}
于 2017-03-23T22:21:14.247 回答