2

c#を使用して、ブート構成データストアから現在実行中のWindowsのインストールの識別子GUIDにアクセスできる必要があります。次のコマンドラインから返すことができます。

bcdedit /enum {current} /v

私が抱えている問題は、c#でこのコマンドを直接実行しようとすると(プログラムが管理者として実行されている場合でも)、bcdeditが存在しないと言われることです。私が使用しているもの:

ProcessStartInfo procStartInfo = new ProcessStartInfo("bcdedit.exe", "/enum {current} /v");

私が調査したもう1つのことは、WMIを使用することですが、これを行うために必要な唯一のリファレンスはhttp://msdn.microsoft.com/en-us/library/windows/desktop/aa362673(v=vs.85).aspxです。これはあまり役に立ちません。

最善の解決策は、bcdeditを使用する必要がなく、代わりにネイティブWMIクラスを使用できる場合です。C#を使用して現在のWindowsブートローダー識別子を見つけるにはどうすればよいですか?

4

2 に答える 2

8

bcdedit.exeに直接アクセスする際に多くの問題があるようですが、C#でWMIを使用して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"));
}

これにより、BcdStore内のすべてのWindows Boot ManagerのGUIDが取得され、メッセージボックスに表示されます。適切なConnectionOptionsが必要であり、このプログラムを管理者として実行する必要があることに注意してください。

http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=18233でBCD定数を検索してくれたRossJohnstonと、 http://wwwで彼のプロジェクトをしてくれたTranDinhHopに感謝します。 .codeproject.com / script / Articles / ViewDownloads.aspx?aid = 19208これには、BcdStoreで動作するすべてのC#コードが含まれています(前述の定数を除く)。

アップデート:

使用:

ManagementObject privateLateBoundObject = new ManagementObject(managementScope, new ManagementPath("root\\WMI:BcdObject.Id=\"{fa926493-6f1c-4193-a414-58f0b2456d1e}\",StoreFilePath=\"\""), null);

現在実行中のWindowsBootManagerのBcdObjectを取得します。その後、電話をかける場合:

currentManObj.GetPropertyValue("Id")

現在のブートマネージャーへのリンクである「{fa926493-6f1c-4193-a414-58f0b2456d1e}」とは異なる、現在実行中のWindowsブートマネージャーのGUIDを取得します。

現在のブートマネージャーにリンクするGUID定数を持っているMicrosoftScriptingGuysとそのプロジェクト(http://technet.microsoft.com/en-us/magazine/2008.07.heyscriptingguy.aspx?pr=blog )に感謝します。

于 2012-02-23T00:13:09.167 に答える
5

%systemroot%\system32には64ビットのbcdedit.exeしかないことに注意してください。アプリが32ビットの場合、WOW64レイヤーがsystem32 \ディレクトリをsyswow64に再マップするため、64ビットのbcdeditを起動できません。WMIインターフェイスを使用するのが間違いなく最善です。

于 2012-08-22T18:07:19.893 に答える