Visual C# を使用してアセンブリをグローバル アセンブリ キャッシュにインストールする

この記事では、Visual C# のグローバル アセンブリ キャッシュにアセンブリをインストールする方法について説明します。

元の製品バージョン: Visual C#
元の KB 番号: 815808

概要

この記事では、アセンブリの厳密な名前を生成する方法と、グローバル アセンブリ キャッシュ (GAC) に .dll ファイルをインストールする方法について説明します。 GAC を使用すると、多くのアプリケーション間でアセンブリを共有できます。 GAC は .NET ランタイムと共に自動的にインストールされます。 通常、コンポーネントは に C:\WINNT\Assembly格納されます。

GAC にアセンブリをインストールするには、アセンブリに厳密な名前を付ける必要があります。 名前は、暗号化ハッシュ キーまたは署名です。 この厳密な名前により、正しいコンポーネントのバージョン管理が保証されます。 これにより、同じ名前のコンポーネントが互いに競合したり、使用中のアプリケーションで誤って使用されたりするのを防ぐことができます。

要件

  • 共有アセンブリがインストールされているコンピューターに対する管理者権限
  • .NET のアセンブリに関する一般的な知識。
  • コマンド プロンプトでのツールの使用に関する一般的な知識。

グローバル アセンブリ キャッシュ

Visual Studio を使用して小さなクラス ライブラリ プロジェクトを作成し、厳密な名前を生成し、GAC にプロジェクトの .dll ファイルをインストールするには、次の手順に従います。

  1. Visual Studio で、新しい Visual C# クラス ライブラリ プロジェクトを作成し、プロジェクトに GACDemo という名前を付けます。

  2. 厳密な名前を使用する必要があります。 この暗号化キー ペアを生成するには、Strong Name ツール (Sn.exe) を使用します。 このツールは、\bin.NET Framework Solution Developer Kit (SDK) がインストールされているサブディレクトリにあります。 Sn.exe ツールは使いやすいです。 コマンド ライン ステートメントは次を受け取ります。

    sn -k "[DriveLetter]:\[DirectoryToPlaceKey]\[KeyName].snk"
    

    注:

    Visual Studio では、IDE プロジェクトのプロパティを使用してキー ペアを生成し、アセンブリに署名できます。 その後、手順 3 と手順 4 をスキップし、 AssemblyInfo.cs ファイルへのコード変更をスキップすることもできます。

    IDE プロジェクト プロパティを使用してキー ペアを生成し、アセンブリに署名するには、次の手順に従います。

    1. ソリューション エクスプローラーで、GACDemo を右クリックし、[プロパティ] をクリックします。

    2. [署名] タブをクリックし、[アセンブリ チェックに署名する] ボックスをクリックして選択します。

    3. [厳密な名前キーの選択] ボックスの一覧で、[新規>]をクリックします<

    4. キー ファイル名として「GACkey.snk」と入力し、[パスワードチェックでキー ファイルを保護する] ボックスをオフにして、[OK] をクリックします。

    5. Ctrl キーを押しながら Shift キーを押しながら B キーを押して、プロジェクトをコンパイルします。

    これらの手順を実行した後でも、手順 5 に従ってアセンブリを GAC にインストールする必要があります。

  3. GACKey という名前の C:\ ディレクトリを作成して、キーを簡単に見つけ、コマンド プロンプトでキーにアクセスできるようにします。

    ほとんどのユーザーにとって、.NET ツールは にあります C:\Program Files\Microsoft.NET\FrameworkSDK\Bin。 次のコマンドを入力する前に、コンピューター上のこの同様のパスを .NET bin ディレクトリにコピーする必要があります。 コマンド プロンプトで「」と入力 cd し、パスを右クリックして貼り付け、Enter キーを押して SN ツールが配置されているディレクトリにすばやく変更します。

    次のコマンドを入力します。

    sn -k "C:\GACKey\GACkey.snk"
    
  4. キーは生成されますが、プロジェクトのアセンブリにはまだ関連付けられません。 この関連付けを作成するには、Visual Studio .NET ソリューション エクスプローラーでAssemblyInfo.cs ファイルをダブルクリックします。 このファイルには、Visual Studio .NET でプロジェクトを作成するときに既定で含まれるアセンブリ属性の一覧が含まれています。 コード内の AssemblyKeyFile アセンブリ属性を次のように変更します。

    [assembly: AssemblyKeyFile('C:\\GACKey\\GACKey.snk') ]
    

    Ctrl キーを押しながら Shift キーを押しながら B キーを押して、プロジェクトをコンパイルします。 GAC に .dll ファイルをインストールするために追加のコードを用意する必要はありません。

  5. .dll ファイルをインストールするには、Gacutil ツールを使用するか、.dll ファイルを適切なフォルダーにドラッグします。 Gacutil ツールを使用する場合は、次のようなコマンドを使用できます。

    gacutil -I "[DriveLetter]:\[PathToBinDirectoryInVSProject]\gac.dll"
    

    ファイルをドラッグするには、Windows エクスプローラーの 2 つのインスタンスを開きます。 1 つのインスタンスで、コンソール プロジェクトの .dll ファイル出力の場所を見つけます。 もう一方のインスタンスで を見つけます 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("")]

検証

  1. エクスプローラーを起動します。
  2. を見つけます C:\SystemRoot\assembly
  3. インストールされている .dll ファイルの一覧に GACDemo が表示されます。

関連情報