c#如何共用組件

來源:互聯網
上載者:User

如何在全域組件快取 (GAC) 中安裝 DLL 檔案要使用 Visual Studio .NET 建立小型類庫項目、產生強式名稱,以及在 GAC 中安裝項目的 .DLL 檔案,請執行下列步驟: 在 Visual Studio .NET 中,建立一個新 Visual C# .NET 類庫項目,並將該項目命名為 GACDemo。 必須使用強式名稱。要產生此加密金鑰對,請使用 SN 工具。此工具位於安裝 .NET Framework 解決方案開發人員工具包 (SDK) 的 \bin 子目錄中。SN 工具便於使用。命令列語句採用以下形式: sn -k "[磁碟機代號]:\[放置密鑰的目錄]\[密鑰名稱].snk" 在 C:\ 中建立一個名為 GACKey 的目錄,以便您可以輕鬆地找到密鑰,並從命令提示字元處訪問該密鑰。

   注意:對於大多數使用者,.NET 工具位於 C:\Program Files\Microsoft.NET\FrameworkSDK\Bin 中。鍵入以下 SN 命令前,可能需要在您的電腦上將與該路徑類似的路徑複製到 .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 "[磁碟機代號]:\[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("")]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.