问题描述
我试图获得与使用 bcdedit 命令bcdedit/enum ALL"获得的信息相同的信息,但使用的是 wmi 和 C#.我知道如何获取 bootmgr 条目(请参阅代码)但我无法获取所有条目,尤其是设备选项是我正在寻找的信息.有没有人知道如何实现这一目标?
I'm trying to get the same information as I get with the bcdedit command "bcdedit /enum ALL" but using wmi and C#. I know how to get the bootmgr entries (see code) but I can't get all entries, especially the device options is the information I'm looking for. Does anyone have an idea how to achieve that?
这是我用来获取标准和传统操作系统引导条目的代码.
This is the code I'm using to get the standard and legacy os boot entries.
public class BCDWMI
{
public static readonly UInt32 BCDE_STANDARD_OS_ENTRY = 0x10200003;
public static readonly UInt32 BCDE_LEGACY_OS_ENTRY = 0x10300006;
public static readonly UInt32 BcdLibraryElementTypeString_Description = 0x12000004;
public static Dictionary<string, string> EnumerateObjectsByType(uint bcdType, string storePath)
{
Dictionary<string, string> dictEntries = null;
ConnectionOptions options = new ConnectionOptions();
options.Impersonation = ImpersonationLevel.Impersonate;
options.EnablePrivileges = true;
ManagementScope MgmtScope = new ManagementScope("root\\WMI", options);
ManagementPath MgmtPath = new ManagementPath("root\\WMI:BcdStore.FilePath='" + storePath + "'");
ManagementObject bcdStore = new System.Management.ManagementObject(MgmtScope, MgmtPath, null);
ManagementBaseObject[] mboArray;
bool success = EnumerateObjects(bcdStore, bcdType, out mboArray);
if (success)
{
dictEntries = new Dictionary<string, string>();
foreach (ManagementBaseObject mbo in mboArray)
{
ManagementPath BcdObjectPath = new ManagementPath("root\\WMI:BcdObject.Id=\"" + mbo.GetPropertyValue("Id") + "\",StoreFilePath='" + storePath + "'");
ManagementObject BcdObject = new ManagementObject(MgmtScope, BcdObjectPath, null);
ManagementBaseObject Element;
String Description = String.Empty;
try
{
bool getDescripStatus = GetElement(BcdObject, BcdLibraryElementTypeString_Description, out Element);
if (getDescripStatus)
Description = Element.GetPropertyValue("String").ToString();
}
catch (Exception)
{
}
dictEntries.Add((string)mbo.GetPropertyValue("Id"), String.Format("Type: {0:X8} {1}", mbo.GetPropertyValue("Type"), Description));
}
}
return dictEntries;
}
public static bool EnumerateObjects(ManagementObject bcdStore, uint Type, out System.Management.ManagementBaseObject[] Objects)
{
System.Management.ManagementBaseObject inParams = null;
inParams = bcdStore.GetMethodParameters("EnumerateObjects");
inParams["Type"] = ((uint)(Type));
System.Management.ManagementBaseObject outParams = bcdStore.InvokeMethod("EnumerateObjects", inParams, null);
Objects = ((System.Management.ManagementBaseObject[])(outParams.Properties["Objects"].Value));
return System.Convert.ToBoolean(outParams.Properties["ReturnValue"].Value);
}
public static bool GetElement(ManagementObject bdcObject, uint Type, out System.Management.ManagementBaseObject Element)
{
System.Management.ManagementBaseObject inParams = null;
inParams = bdcObject.GetMethodParameters("GetElement");
inParams["Type"] = ((uint)(Type));
System.Management.ManagementBaseObject outParams = bdcObject.InvokeMethod("GetElement", inParams, null);
Element = ((System.Management.ManagementBaseObject)(outParams.Properties["Element"].Value));
return System.Convert.ToBoolean(outParams.Properties["ReturnValue"].Value);
}
}
为了查询系统存储,我像这样调用函数.
To query the system store, I call the function like this.
Dictionary<string, string> StdOSEntries = BCDWMI.EnumerateObjectsByType(BCDWMI.BCDE_STANDARD_OS_ENTRY, String.Empty);
foreach (String guid in StdOSEntries.Keys)
Debug.WriteLine(String.Format("Id={0} {1}", guid, StdOSEntries[guid]));
推荐答案
如果您需要了解感兴趣的启动项的类型值,可以使用 bcdedit.exe 查找该项的 GUID,并且然后在您的程序中加载该条目,将 GUID 放在 ManagementPath 中,就像您在:
If you need to learn the type value of a boot entry you are interested in, you can use bcdedit.exe to look up the GUID of that entry, and then load that entry in your program putting the GUID in ManagementPath like you do in:
ManagementPath BcdObjectPath = new ManagementPath("root\\WMI:BcdObject.Id=\"" + "{Guid goes here}" + "\",StoreFilePath='" + storePath + "'");
然后像这样获取 Type 属性的值:
then get the value of a Type property like this:
uint Type = (uint)BcdObject["Type"];
Console.WriteLine("{0:X8}", Type);
如果 bcdedit.exe 显示众所周知的"标识符,如 {current} 或 {bootmgr},请使用 本文档 将标识符映射到 GUID.
In case if bcdedit.exe shows "well known" identifier like {current} or {bootmgr}, use this document to map the identifier to a GUID.
您也可以像这样以编程方式枚举类型:
You can also enumerate types programmatically like this:
for (uint a = 1; a <= 10; a++) {
uint FirmwareType = 0x10100000 | a;
uint BootType = 0x10200000 | a;
uint LegacyType = 0x10300000 | a;
uint RealModeType = 0x10400000 | a;
}
它会给出像你在这里一样的标识符
which will give identifiers like you have here
public static readonly UInt32 BCDE_STANDARD_OS_ENTRY = 0x10200003;
public static readonly UInt32 BCDE_LEGACY_OS_ENTRY = 0x10300006;
请参阅 WMI 文档BcdObject 类,用于描述这些标识符的形成方式.
See WMI documentation on BcdObject class for description of how those identifiers are formed.
这篇关于如何获得与使用 wmi 使用 bcdedit/enum ALL 获得的结果相同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!