共用方式為


使用 Visual C# 將元件安裝到全域程式集緩存

本文提供如何在Visual C# 中將元件安裝至全域程式集緩存的資訊。

原始產品版本: Visual C#
原始 KB 編號: 815808

摘要

本文說明如何產生元件的強名稱,以及如何在全域程式集緩存中安裝.dll檔案。 透過 GAC,您可以跨許多應用程式共用元件。 GAC 會自動與 .NET 運行時間一起安裝。 元件通常會儲存在 中 C:\WINNT\Assembly

若要在 GAC 中安裝元件,您必須為元件提供強名稱。 名稱是密碼編譯哈希密鑰或簽章。 這個強名稱可確保正確的元件版本控制。 這有助於防止具有相同名稱的元件彼此衝突,或被取用的應用程式不當使用。

需求

  • 安裝共用元件之計算機的系統管理員許可權
  • 一般熟悉 .NET 中的元件。
  • 在命令提示字元中熟悉工具的使用。

全域組件快取

若要使用 Visual Studio 建立小型類別庫專案、產生強名稱,以及在 GAC 中安裝專案的.dll檔案,請遵循下列步驟:

  1. 在 Visual Studio 中,建立新的 Visual C# 類別庫專案,並將專案 命名為 GACDemo

  2. 您必須使用強名稱。 若要產生此密碼編譯密鑰組,請使用強名稱工具 (Sn.exe)。 此工具位於 \bin 安裝 .NET Framework 解決方案開發人員套件 (SDK) 的子目錄中。 Sn.exe工具很容易使用。 命令行語句會採用下列命令

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

    注意

    在 Visual Studio 中,您可以使用 IDE 專案屬性來產生金鑰組並簽署元件。 然後,您可以略過步驟 3 和步驟 4,也可以略過對 AssemblyInfo.cs 檔案進行任何程式代碼變更。

    若要使用 IDE 專案屬性來產生金鑰組並簽署元件,請遵循下列步驟:

    1. 在 [方案總管] 中,以滑鼠右鍵按兩下 [GACDemo],然後按兩下 [屬性]。

    2. 按兩下 [簽署] 索引標籤,然後按下以選取 [簽署元件] 複選框。

    3. 在 [ 選擇強名稱金鑰 ] 清單中,按兩下 [ <新增...>]。

    4. 輸入 GACkey.snk 作為金鑰檔名,清除 [ 使用密碼 保護我的密鑰檔案] 複選框,然後按下 [ 確定]。

    5. 按 CTRL+SHIFT+B 鍵盤快捷方式來編譯專案。

    遵循這些步驟之後,您仍然必須遵循步驟 5,才能在 GAC 中安裝元件。

  3. 在 中C:\建立名為 GACKey 的目錄,以便您可以輕鬆地找到金鑰,並在命令提示字元存取密鑰。

    對大部分的使用者而言,.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. 您可以使用 Gacutil 工具或將.dll檔案拖曳至適當的資料夾,來安裝.dll檔案。 如果您使用 Gacutil 工具,可以使用類似下列的命令:

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

    若要拖曳檔案,請開啟 Windows 檔案總管的兩個實例。 在一個實例中,尋找控制台專案.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. 啟動 Windows 檔案總管。
  2. 找出 C:\SystemRoot\assembly
  3. 您會在已安裝.dll檔案清單中看到 GACDemo

參考資料