从提升的命令提示符下运行bcdedit.exe时,您可以看到当前BCD设置的值。我需要阅读“ hypervisorlaunchtype ”的设置/值

有人知道这样做的方法吗?

我试图将管道输出写入tmp文件,以便可以对其进行解析,但是由于bcdedit.exe需要从提升的提示符下运行这一事实而遇到管道输出问题。也许有更好的方法?

编辑:我忘了补充一点,我希望能够做到这一点,而最终用户根本看不到命令​​提示符(即什至没有快速闪烁)。

最佳答案

首先,以管理员身份运行Visual Studio,并在控制台应用程序中尝试以下代码(通过调试运行该应用程序):

    static void Main(string[] args)
    {

        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.FileName = @"CMD.EXE";
        p.StartInfo.Arguments = @"/C bcdedit";
        p.Start();
        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        // parse the output
        var lines = output.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Where(l => l.Length > 24);
        foreach (var line in lines)
        {
            var key = line.Substring(0, 24).Replace(" ", string.Empty);
            var value = line.Substring(24).Replace(" ", string.Empty);
            Console.WriteLine(key + ":" + value);
        }
        Console.ReadLine();
    }

但是,存在一个问题,如果您希望从提升的Visual Studio外部启动应用程序时该方法能够正常工作,则需要配置您的应用程序以请求提升的权限:

在您的项目上,单击“添加新项”,然后选择“应用程序 list 文件”。

打开app.manifest文件并替换以下行:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />

与此:
<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

10-05 18:29
查看更多