无需管理员即可从c#以编程方式检测BitLocker

Kev*_*onn 7 c# bitlocker

从各种线程我拼凑如何以编程方式检查BitLocker像这样:

private void TestBitLockerMenuItem_Click(object sender, RoutedEventArgs e) {
   var path=new ManagementPath(@"\ROOT\CIMV2\Security\MicrosoftVolumeEncryption")
               { ClassName="Win32_EncryptableVolume" };
   var scope=new ManagementScope(path);
   path.Server=Environment.MachineName;
   var objectSearcher=new ManagementClass(scope, path, new ObjectGetOptions());
   foreach (var item in objectSearcher.GetInstances()) {
      MessageBox.Show(item["DeviceID"].ToString()+" "+item["ProtectionStatus"].ToString());
   }
}
Run Code Online (Sandbox Code Playgroud)

但它仅在进程具有管理员权限时才有效.

看起来奇怪的是,任何旧的Windows用户都可以转到资源管理器,右键单击某个驱动器,然后查看它是否已启用BitLocker,但程序似乎无法完成此操作.有谁知道这样做的方法?

sly*_*ete 8

Windows通过使用Win32 API中的Windows Property System来检查未记录的shell属性,从而在shell中显示此信息System.Volume.BitLockerProtection.您的程序也可以在没有高程的情况下检查此属性.

如果此属性的值为1,3或5,则在驱动器上启用BitLocker.任何其他值都被视为关闭.

在我搜索此问题的解决方案期间,我发现了对此shell属性的引用HKEY_CLASSES_ROOT\Drive\shell\manage-bde\AppliesTo.最终,这一发现引导我找到这个解决方案.

Windows Property System是一个低级API,但您可以使用Windows API代码包中提供的包装器.

Install-Package WindowsAPICodePack
Run Code Online (Sandbox Code Playgroud)

运用

using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
Run Code Online (Sandbox Code Playgroud)

IShellProperty prop = ShellObject.FromParsingName("C:").Properties.GetProperty("System.Volume.BitLockerProtection");
int? bitLockerProtectionStatus = (prop as ShellProperty<int?>).Value;

if (bitLockerProtectionStatus.HasValue && (bitLockerProtectionStatus == 1 || bitLockerProtectionStatus == 3 || bitLockerProtectionStatus == 5))
   Console.WriteLine("ON");
else
   Console.WriteLine("OFF");
Run Code Online (Sandbox Code Playgroud)

  • 调试时运行良好,但在 Windows 服务中运行时失败。服务在哪个用户下运行并不重要:本地系统、本地服务或管理员帐户。知道可能是什么问题或解决方法吗? (2认同)