使用 C# 访问 Windows 7 启动配置数据
我需要能够使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
直接访问 bcdedit.exe 似乎有很多问题,但我能够弄清楚如何在 C# 中使用 WMI 来访问 BcdStore:
这会获取 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# 代码(上述常量除外)。
更新:
使用:
将获取当前正在运行的 Windows 启动管理器的 BcdObject。如果您随后调用:
您将获得当前正在运行的 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:
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:
will obtain the BcdObject for the current, running Windows Boot Manager. If you then call:
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.
注意%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.