この記事では、Visual C# のグローバル アセンブリ キャッシュにアセンブリをインストールする方法について説明します。
元の製品バージョン: Visual C#
元の KB 番号: 815808
まとめ
この記事では、アセンブリの厳密な名前を生成する方法と、グローバル アセンブリ キャッシュ (GAC) に.dll ファイルをインストールする方法について説明します。 GAC を使用すると、多くのアプリケーションでアセンブリを共有できます。 GAC は.NET ランタイムと共に自動的にインストールされます。 コンポーネントは通常、 C:\WINNT\Assembly
に格納されます。
GAC にアセンブリをインストールするには、アセンブリに厳密な名前を付ける必要があります。 名前は暗号化ハッシュ キーまたは署名です。 この厳密な名前により、正しいコンポーネントのバージョン管理が保証されます。 これにより、同じ名前のコンポーネントが相互に競合したり、使用中のアプリケーションで誤って使用されたりするのを防ぐことができます。
要件
- 共有アセンブリがインストールされているコンピューターに対する管理者権限
- .NET のアセンブリに関する一般的な知識。
- コマンド プロンプトでのツールの使用に関する一般的な知識。
グローバル アセンブリ キャッシュ
Visual Studio を使用して小規模なクラス ライブラリ プロジェクトを作成し、厳密な名前を生成し、GAC にプロジェクトの.dll ファイルをインストールするには、次の手順に従います。
Visual Studio で、新しい Visual C# クラス ライブラリ プロジェクトを作成し、プロジェクトに GACDemo という名前を付けます。
厳密な名前を使用する必要があります。 この暗号化キー ペアを生成するには、厳密な名前ツール (Sn.exe) を使用します。 このツールは、.NET Framework Solution Developer Kit (SDK) がインストールされている
\bin
サブディレクトリにあります。 Sn.exeツールは使いやすいです。 コマンド ライン ステートメントでは、次の処理が行われます。sn -k "[DriveLetter]:\[DirectoryToPlaceKey]\[KeyName].snk"
Note
Visual Studio では、IDE プロジェクトのプロパティを使用してキー ペアを生成し、アセンブリに署名できます。 その後、手順 3 と手順 4 をスキップし、 AssemblyInfo.cs ファイルにコード変更を加えるのもスキップできます。
IDE プロジェクトのプロパティを使用してキー ペアを生成し、アセンブリに署名するには、次の手順に従います。
ソリューション エクスプローラーで、GACDemoを右クリックし、[プロパティの] をクリック。
[ 署名 ] タブをクリックし、[ アセンブリの署名 ] チェック ボックスをオンにします。
厳密な名前キーを選択します一覧で、<New...> をクリックします。
キー ファイル名として「 GACkey.snk 」と入力し、[ パスワードでキー ファイルを保護する ] チェック ボックスをオフにして、[ OK] をクリック。
Ctrl キーを押しながら Shift キーを押しながら B キーを押して、プロジェクトをコンパイルします。
これらの手順を実行した後も、手順 5 に従ってアセンブリを GAC にインストールする必要があります。
C:\
に GACKey という名前のディレクトリを作成して、キーを簡単に見つけて、コマンド プロンプトでキーにアクセスできるようにします。ほとんどのユーザーにとって、.NET ツールは
C:\Program Files\Microsoft.NET\FrameworkSDK\Bin
にあります。 次のコマンドを入力する前に、コンピューター上のこの同様のパスを .NET bin ディレクトリにコピーできます。 コマンド プロンプトで「cd
」と入力し、右クリックしてパスを貼り付け、Enter キーを押して SN ツールが配置されているディレクトリにすばやく移動します。次のコマンドを入力します。
sn -k "C:\GACKey\GACkey.snk"
キーは生成されますが、プロジェクトのアセンブリにまだ関連付けられません。 この関連付けを作成するには、Visual Studio .NET ソリューション エクスプローラーでAssemblyInfo.cs ファイルをダブルクリックします。 このファイルには、Visual Studio .NET でプロジェクトを作成するときに既定で含まれるアセンブリ属性の一覧が含まれています。 コード内の
AssemblyKeyFile
アセンブリ属性を次のように変更します。[assembly: AssemblyKeyFile('C:\\GACKey\\GACKey.snk') ]
Ctrl キーを押しながら Shift キーを押しながら B キーを押して、プロジェクトをコンパイルします。 GAC に.dll ファイルをインストールするために追加のコードを用意する必要はありません。
gacutil ツールを使用するか、.dll ファイルを適切なフォルダーにドラッグして、.dll ファイルをインストールできます。 Gacutil ツールを使用する場合は、次のようなコマンドを使用できます。
gacutil -I "[DriveLetter]:\[PathToBinDirectoryInVSProject]\gac.dll"
ファイルをドラッグするには、Windows エクスプローラーの 2 つのインスタンスを開きます。 1 つのインスタンスで、コンソール プロジェクトの.dll ファイル出力の場所を見つけます。 もう 1 つのインスタンスで、
c:\<SystemRoot>\Assembly
を見つけます。 次に、.dll ファイルを Assembly フォルダーにドラッグします。
完全なコード一覧 (AssemblyInfo.cs)
using System.Reflection;
using System.Runtime.CompilerServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// that is associated with an assembly.
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Version information for an assembly is made up of the following four values:
// Major Version
// Minor Version
// Build Number
// Revision
// You can specify all the values, or you can default the revision and build numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.*")]
// To sign your assembly you must specify a key to use. See the
// Microsoft .NET Framework documentation for more information about assembly signing.
// Use the following attributes to control that key is used for signing.
// Notes:
// (*) If no key is specified, the assembly is not signed.
// (*) KeyName refers to a key that has been installed in the Crypto Service
// Provider (CSP) on your computer. KeyFile refers to a file that contains
// a key.
// (*) If the KeyFile and the KeyName values are both specified, the
// following processing occurs:
// (1) If the KeyName can be found in the CSP, that key is used.
// (2) If the KeyName does not exist and the KeyFile does exist, the key
// in the KeyFile is installed to the CSP and used.
// (*) To create a KeyFile, you can use the sn.exe (Strong Name) utility.
// When specifying the KeyFile, the location of the KeyFile must be
// relative to the project output directory which is
// %Project Directory%\obj\<configuration>. For example, if your KeyFile is
// located in the project directory, you would specify the AssemblyKeyFile
// attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
// (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
// documentation for more information about this.
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("C:\\GACKey\\GACKey.snk")]
[assembly: AssemblyKeyName("")]
検証
- Windows Explorer を起動します。
C:\SystemRoot\assembly
を見つけます。- インストールされている.dll ファイルの一覧に GACDemo が表示されます。