Access the Windows 7 Boot Configuration Data using C#

前端 未结 2 2053
臣服心动
臣服心动 2021-01-07 05:42

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 c

相关标签:
2条回答
  • 2021-01-07 05:59

    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.

    0 讨论(0)
  • 2021-01-07 06:16

    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.

    0 讨论(0)
提交回复
热议问题