程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 如何在全局程序集緩存(GAC)中安裝DLL文件

如何在全局程序集緩存(GAC)中安裝DLL文件

編輯:C#入門知識

  如何在全局程序集緩存 (GAC) 中安裝 DLL 文件要使用 Visual Studio .NET 創建小型類庫項目、生成強名稱,以及在 GAC 中安裝項目的 .dll 文件,請執行下列步驟: 在 Visual Studio .NET 中,創建一個新 Visual C# .NET 類庫項目,並將該項目命名為 GACDemo。 必須使用強名稱。要生成此加密密鑰對,請使用 SN 工具。此工具位於安裝 .NET Framework 解決方案開發人員工具包 (SDK) 的 in 子目錄中。SN 工具易於使用。命令行語句采用以下形式: sn -k "[驅動器號]:[放置密鑰的目錄][密鑰名稱].snk" 在 C: 中創建一個名為 GACKey 的目錄,以便您可以輕松地找到密鑰,並從命令提示符處訪問該密鑰。

   注意:對於大多數用戶,.NET 工具位於 C:Program FilesMicrosoft.NETFrameworkSDKBin 中。鍵入以下 SN 命令前,可能需要在您的計算機上將與該路徑類似的路徑復制到 .NET bin 目錄中。從命令提示符處鍵入 cd,右鍵單擊以粘貼該路徑,然後按 ENTER 鍵,快速轉至 SN 工具所在的目錄。

   鍵入以下內容: sn -k "C:GACKeyGACkey.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 "[驅動器號]:[VS 項目中 Bin 目錄的路徑]gac.dll"如果您要拖動文件,請使用 Microsoft Windows 資源管理器。打開 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. 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. 下一頁:
Copyright © 程式師世界 All Rights Reserved