none
How can I change win7 Boot Configuration Data(BCD) by C# RRS feed

  • 问题

  • I want change the BCD of windows7 by VS2010 and C#,which API should be used ?

    can you give me a example if you know.

    thanks!

    2012年7月30日 3:59

答案

  • Hi Rytian,

    Please try WMI way: http://msdn.microsoft.com/en-us/library/windows/desktop/aa362677(v=vs.85).aspx

    And the sample code is here: http://stackoverflow.com/questions/9337354/access-the-windows-7-boot-configuration-data-using-c-sharp

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

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 Rytian 2012年7月31日 7:51
    2012年7月31日 1:51
    版主

全部回复

  • Hi Rytian,

    Please try WMI way: http://msdn.microsoft.com/en-us/library/windows/desktop/aa362677(v=vs.85).aspx

    And the sample code is here: http://stackoverflow.com/questions/9337354/access-the-windows-7-boot-configuration-data-using-c-sharp

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

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 Rytian 2012年7月31日 7:51
    2012年7月31日 1:51
    版主
  • thank you !
    2012年7月31日 7:52