本文介绍如何在 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)。 此工具位于
\bin
安装 .NET Framework 解决方案开发人员工具包(SDK)的子目录中。 Sn.exe工具易于使用。 命令行语句采用以下操作sn -k "[DriveLetter]:\[DirectoryToPlaceKey]\[KeyName].snk"
注意
在 Visual Studio 中,可以使用 IDE 项目属性生成密钥对并对程序集进行签名。 然后,可以跳过步骤 3 和步骤 4,并跳过对 AssemblyInfo.cs 文件进行任何代码更改。
若要使用 IDE 项目属性生成密钥对并对程序集进行签名,请执行以下步骤:
在解决方案资源管理器中,右键单击 GACDemo,然后单击“属性”。
单击“签名”选项卡,然后单击以选中“对程序集进行签名”复选框。
在 “选择强名称密钥 列表”中,单击“ <新建...”>。
键入 GACkey.snk 作为密钥文件名,清除“ 使用密码 保护密钥文件”复选框,然后单击“ 确定”。
按 Ctrl+Shift+B 键盘快捷方式编译项目。
执行这些步骤后,仍必须按照步骤 5 在 GAC 中安装程序集。
创建名为 GACKey 的
C:\
目录,以便可以轻松找到密钥,并在命令提示符处访问密钥。对于大多数用户,.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 资源管理器的两个实例。 在一个实例中,找到控制台项目的.dll文件输出的位置。 在其他实例中,找到
c:\<SystemRoot>\Assembly
。 然后,将.dll文件拖到“程序集”文件夹中。
完整代码列表 (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 资源管理器。
- 找到
C:\SystemRoot\assembly
。 - 在已安装的 .dll 文件中看到 GACDemo 。