Я пытаюсь получить ту же информацию, что и с помощью команды bcdedit «bcdedit /enum ALL», но с помощью wmi и C#. Я знаю, как получить записи bootmgr (см. код), но я не могу получить все записи, особенно параметры устройства — это информация, которую я ищу. Кто-нибудь знает, как этого добиться?
Это код, который я использую для получения стандартных и устаревших загрузочных записей ОС.
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);
}
}
Чтобы запросить системное хранилище, я вызываю функцию следующим образом.
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]));
EnumerateElements
на своемManagementObject
и устанавливаете точку останова, вы можете проверить результат и скопировать его в свой вопрос, пожалуйста. - person Hackerman   schedule 31.08.2017