使用C#BCD WMI提供程序来安全地阻止Windows
我已经在网上搜索了如何使用C#将SafeBoot引入Windows的解决方案。 从Vista及以上版本开始,使用BCD控制安全启动。 当然你可以使用命令行工具“bcdedit”:
bcdedit /set {current} safeboot Minimal
但是我不想使用这种方法。 所以我的问题是:
如何仅使用C#重启进入安全模式?
我已经看过这个SOpost了 ,这让我开始了。 但我仍然缺少这个难题的碎片。
任何帮助是极大的赞赏。 =)
BCD WMI Provider Reference几乎没有帮助。
我在C#中编写了以下代码,允许您设置safeboot值并删除该值:
using System; using System.Collections.Generic; using System.Linq; using System.Management; using System.Text; using System.Threading.Tasks; namespace EditBcdStore { public class BcdStoreAccessor { public const int BcdOSLoaderInteger_SafeBoot = 0x25000080; public enum BcdLibrary_SafeBoot { SafemodeMinimal = 0, SafemodeNetwork = 1, SafemodeDsRepair = 2 } private ConnectionOptions connectionOptions; private ManagementScope managementScope; private ManagementPath managementPath; public BcdStoreAccessor() { connectionOptions = new ConnectionOptions(); connectionOptions.Impersonation = ImpersonationLevel.Impersonate; connectionOptions.EnablePrivileges = true; managementScope = new ManagementScope("root\\WMI", connectionOptions); managementPath = new ManagementPath("root\\WMI:BcdObject.Id=\"{fa926493-6f1c-4193-a414-58f0b2456d1e}\",StoreFilePath=\"\""); } public void SetSafeboot() { ManagementObject currentBootloader = new ManagementObject(managementScope, managementPath, null); currentBootloader.InvokeMethod("SetIntegerElement", new object[] { BcdOSLoaderInteger_SafeBoot, BcdLibrary_SafeBoot.SafemodeMinimal }); } public void RemoveSafeboot() { ManagementObject currentBootloader = new ManagementObject(managementScope, managementPath, null); currentBootloader.InvokeMethod("DeleteElement", new object[] { BcdOSLoaderInteger_SafeBoot }); } } }
我在Surface Pro上进行了测试,看起来很有效,可以通过运行来validation:
bcdedit /enum {current} /v
更新:
上面的代码仅用于设置或删除允许您安全启动的值。
执行此操作后,需要重新启动,这也可以使用WMI完成,如下所示:
WMI重启远程计算机
答案显示了本地或远程执行此操作的示例。
非常感谢Helen和L-Williams。