使用C#BCD WMI提供了SafeBoot的Windows [英] Use C# BCD WMI Provider to SafeBoot Windows

查看:379
本文介绍了使用C#BCD WMI提供了SafeBoot的Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经走遍了网上寻找如何只用C#来了SafeBoot到Windows的解决方案。自Vista和以上,安全引导是使用BCD码控制。 Ofcourse您可以使用命令行工具BCDEDIT:

  BCDEDIT /集{当前} SAFEBOOT最少

不过,我不希望用这种方式。所以我的问题是:



如何重新启动到安全模式下仅使用C#



我已经看了这个SO发布,这已经让我开始。但我仍然缺少的部分这个谜题。



任何帮助是极大的赞赏。 =)



BCD WMI提供程序参考是没有多大帮助。


解决方案

我写了在C#下面的代码应该允许您设置SAFEBOOT值,并删除值:



<预类=郎-CS prettyprint-覆盖> 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Management;
使用System.Text;使用System.Threading.Tasks
;

命名空间EditBcdStore
{
公共类BcdStoreAccessor
{
公共const int的BcdOSLoaderInteger_SafeBoot = 0x25000080;

公共枚举BcdLibrary_SafeBoot
{
SafemodeMinimal = 0,
SafemodeNetwork = 1,
SafemodeDsRepair = 2
}

私人ConnectionOptions connectionOptions;
私人管理范围管理范围;
私人ManagementPath managementPath;

公共BcdStoreAccessor()
{
connectionOptions =新ConnectionOptions();
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
connectionOptions.EnablePrivileges = TRUE;

=管理范围新的管理范围(root\\WMI,connectionOptions);

managementPath =新ManagementPath(root\\WMI:BcdObject.Id = \{fa926493-6f1c-4193-a414-58f0b2456d1e} \,StoreFilePath = \\ );
}

公共无效SetSafeboot()
{
的ManagementObject currentBootloader =新的ManagementObject(管理范围,managementPath,NULL);
currentBootloader.InvokeMethod(SetIntegerElement,新的对象[] {BcdOSLoaderInteger_SafeBoot,BcdLibrary_SafeBoot.SafemodeMinimal});
}

公共无效RemoveSafeboot()
{
的ManagementObject currentBootloader =新的ManagementObject(管理范围,managementPath,NULL);
currentBootloader.InvokeMethod(DeleteElement,新的对象[] {BcdOSLoaderInteger_SafeBoot});
}
}
}



我测试这对我的面Pro和它似乎工作,因为可以通过运行来验证:

  BCDEDIT /枚举{当前} / v 

更新:



上面的代码仅仅是设置或删除,让您SAFEBOOT值。



这已经执行后,需要重新启动,也可使用WMI为完成如下所示:



WMI重新启动远程机器



答案示出了用于本地或远程执行这样的一个例子。



非常感谢海伦和L - 威廉姆斯。


I have scoured the web looking for solutions on how to SafeBoot into Windows using only C#. Since Vista and above, safe booting is controlled using BCD. Ofcourse you could use the commandline tool "bcdedit":

bcdedit /set {current} safeboot Minimal

However I do not want to use this approach. So my question is:

How do I reboot into safe mode using only C#?

I have already looked at this SO post, which has got me started. But I'm still missing pieces to this puzzle.

Any help is greatly appreciated. =)

BCD WMI Provider Reference is of little help.

解决方案

I wrote up the following code in C# that should allow you to set the safeboot value and delete that value:

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

I tested this on my Surface Pro and it seemed to work, as can be verified by running:

bcdedit /enum {current} /v

Update:

The code above is just for setting or removing the value that allows you to safeboot.

After this has been performed, a reboot is required, which can also be accomplished using WMI as is shown here:

WMI to reboot remote machine

The answer shows an example for performing this locally or remotely.

Big thanks to Helen and L-Williams.

这篇关于使用C#BCD WMI提供了SafeBoot的Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆