使用 C# 访问 Windows 7 启动配置数据

发布于 2025-01-07 03:07:27 字数 625 浏览 1 评论 0原文

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

bcdedit /enum {current} /v

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

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

我研究的另一件事是使用 WMI,但我必须这样做的唯一参考是 http://msdn.microsoft.com/en-us/library/windows/desktop/aa362673(v=vs.85).aspx 这不是很有帮助。

最好的解决方案是我不必使用 bcdedit 而可以使用本机 WMI 类。如何使用 C# 查找当前的 Windows 引导加载程序标识符?

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

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

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.

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#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒绊 2025-01-14 03:07:27

直接访问 bcdedit.exe 似乎有很多问题,但我能够弄清楚如何在 C# 中使用 WMI 来访问 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,并且该程序必须以管理员身份运行。

感谢 Ross Johnston 的项目:http://www.codeproject。 com/script/Articles/ViewDownloads.aspx?aid=18233 查找 BCD 常量并联系 Tran Dinh Hop 的项目:http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid =19208 其中包含与 BcdStore 一起使用的所有 C# 代码(上述常量除外)。

更新:

使用:

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

将获取当前正在运行的 Windows 启动管理器的 BcdObject。如果您随后调用:

currentManObj.GetPropertyValue("Id")

您将获得当前正在运行的 Windows 启动管理器的 GUID,它与“{fa926493-6f1c-4193-a414-58f0b2456d1e}”不同,后者是指向当前启动管理器的链接。

感谢 Microsoft 脚本专家及其项目:http: //technet.microsoft.com/en-us/magazine/2008.07.heyscriptingguy.aspx?pr=blog 用于拥有链接到当前的 GUID 常量启动管理器。

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

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.

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).

Update:

Using:

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

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

currentManObj.GetPropertyValue("Id")

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.

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.

再见回来 2025-01-14 03:07:27

注意%systemroot%\system32下只有64位的bcdedit.exe。如果您的应用程序是 32 位,则它将无法启动 64 位 bcdedit,因为 WOW64 层将 system32\ 目录重新映射到 syswow64。最好使用 WMI 界面。

Note that there is only a 64-bit bcdedit.exe in %systemroot%\system32. If your app is 32-bit, it will not be able to launch the 64-bit bcdedit because the WOW64 layer remaps the system32\ directory to syswow64. It's definitely best to use the WMI interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文