使用C#访问Windows 7引导配置数据 [英] Access the Windows 7 Boot Configuration Data using C#

查看:70
本文介绍了使用C#访问Windows 7引导配置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够使用c#从启动配置数据存储中访问Windows当前正在运行的安装的标识符GUID.可以从运行以下命令的命令行中返回它:

I need to be able to access the identifier GUID of the current running installation of Windows from the Boot Configuration Data Store using c#. It can be returned from the command line running:

bcdedit /enum {current} /v

我遇到的问题是,在c#中,如果我尝试直接运行此命令(即使程序以管理员身份运行),就会被告知bcdedit不存在.我正在使用:

The problem I have is that in c# if I try to directly run this command (even though the program is running as Administrator) I'm told that bcdedit does not exist. I'm using:

ProcessStartInfo procStartInfo = new ProcessStartInfo("bcdedit.exe", "/enum {current} /v");

我研究的另一件事是使用WMI,但我要做的唯一参考是

The other thing that I have researched is using WMI but the only reference I have to doing so is http://msdn.microsoft.com/en-us/library/windows/desktop/aa362673(v=vs.85).aspx which isn't very helpful.

最好的解决方案是,如果我不必使用bcdedit而是可以使用本机WMI类.如何使用C#查找当前的Windows Boot Loader标识符?

The best solution would be if I don't have to use bcdedit but instead could use native WMI classes. How would I find the current Windows Boot Loader identifier using C#?

推荐答案

直接访问bcdedit.exe似乎有很多问题,但是我能够弄清楚如何在C#中使用WMI来访问BcdStore:

There seem to be many problems accessing bcdedit.exe directly but I was able to figure out how to use WMI in C# to access the BcdStore:

ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
connectionOptions.EnablePrivileges = true;

// The ManagementScope is used to access the WMI info as Administrator
ManagementScope managementScope = new ManagementScope(@"root\WMI", connectionOptions);

// {9dea862c-5cdd-4e70-acc1-f32b344d4795} is the GUID of the System BcdStore
ManagementObject privateLateBoundObject = new ManagementObject(managementScope, new ManagementPath("root\\WMI:BcdObject.Id=\"{9dea862c-5cdd-4e70-acc1-f32b344d4795}\",StoreFilePath=\"\""), null);

ManagementBaseObject inParams = null;
inParams = privateLateBoundObject.GetMethodParameters("GetElement");

// 0x24000001 is a BCD constant: BcdBootMgrObjectList_DisplayOrder
inParams["Type"] = ((UInt32)0x24000001);
ManagementBaseObject outParams = privateLateBoundObject.InvokeMethod("GetElement", inParams, null);
ManagementBaseObject mboOut = ((ManagementBaseObject)(outParams.Properties["Element"].Value));

string[] osIdList = (string[]) mboOut.GetPropertyValue("Ids");

// Each osGuid is the GUID of one Boot Manager in the BcdStore
foreach (string osGuid in osIdList)
{
    ManagementObject currentManObj = new ManagementObject(managementScope, new ManagementPath("root\\WMI:BcdObject.Id=\"" + osGuid + "\",StoreFilePath=\"\""), null);
            MessageBox.Show("" + currentManObj.GetPropertyValue("Id"));
}

这将获取BcdStore中每个Windows引导管理器的GUID,并将其显示在MessageBox中.请注意,您必须具有正确的ConnectionOptions,并且该程序必须以管理员身份运行.

This gets the GUID of every Windows Boot Manager in the BcdStore and shows them in a MessageBox. It should be noted that you must have the right ConnectionOptions and that this program must be run as Administrator.

感谢Ross Johnston的项目,网址为: http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid = 18233 来找到BCD常量,并在以下网址找到Tran Dinh Hop的项目:

Thanks to Ross Johnston for his project at: http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=18233 to find the BCD constants and to Tran Dinh Hop for his project at: http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=19208 which has all of the C# code to work with the BcdStore (except for the aforementioned constants).

更新:

使用:

ManagementObject privateLateBoundObject = new ManagementObject(managementScope, new ManagementPath("root\\WMI:BcdObject.Id=\"{fa926493-6f1c-4193-a414-58f0b2456d1e}\",StoreFilePath=\"\""), null);

将获取当前正在运行的Windows启动管理器的BcdObject.如果您再致电:

will obtain the BcdObject for the current, running Windows Boot Manager. If you then call:

currentManObj.GetPropertyValue("Id")

您将获得当前正在运行的Windows引导管理器的GUID,该GUID与"{fa926493-6f1c-4193-a414-58f0b2456d1e}"(它是指向当前引导管理器的链接)不同.

you will get the GUID of the current, running Windows Boot Manager which is different from "{fa926493-6f1c-4193-a414-58f0b2456d1e}" which is a link to the current Boot Manager.

感谢Microsoft脚本专家及其在以下项目中的项目: http://technet.microsoft.com/zh-cn/magazine/2008.07.heyscriptingguy.aspx?pr=blog ,以获取链接到当前启动管理器的GUID常量.

Thanks to The Microsoft Scripting Guys and their project at: http://technet.microsoft.com/en-us/magazine/2008.07.heyscriptingguy.aspx?pr=blog for having that GUID constant that links to the current Boot Manager.

这篇关于使用C#访问Windows 7引导配置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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