创建一个强名称密钥文件+ 如何在 Visual C# .NET 中将程序集安装到全局程序集缓存中...

创建强名称密钥文件
  1. 单击“开始”,依次指向“程序”、“Microsoft Visual Studio 2005”、“Visual Studio 工具”,然后单击“ Visual Studio 2005 命令提示”。

    2 .  在命令提示符下,键入 sn -k Tutorial.snk,然后按 Enter。 
    这里关键的是 sn -k *.snk

From: http://support.microsoft.com/?scid=kb;zh-cn;815808&spid=1108&sid=152

如何在 Visual C# .NET 中将程序集安装到全局程序集缓存中

概要
本文介绍如何为程序集生成强名称,以及如何在全局程序集缓存 (GAC) 中安装 DLL 文件。通过 GAC,您可以在许多应用程序之间共享程序集。GAC 随 .NET 运行库一起自动安装。组件通常存储在 C:\WINNT\Assembly 中。

要在 GAC 中安装程序集,必须为程序集指定强名称。该名称是加密的哈希密钥或签名。这种强名称可确保正确的组件版本控制。这有助于防止具有相同名称的组件相互冲突或被消耗应用程序误用。

要求
下面概括列出了推荐的硬件、软件、网络结构以及所需的 Service Pack: • 对安装共享程序集的计算机的管理员权限

本文假定您熟悉下列主题: • 大体熟悉 .NET 中的程序集
• 大体熟悉从命令提示符处使用工具

全局程序集缓存
要使用 Visual Studio .NET 创建小型类库项目、生成强名称,以及在 GAC 中安装项目的 .dll 文件,请按照下列步骤操作: 1. 在 Visual Studio .NET 中,创建一个新 Visual C# .NET 类库项目,并将该项目命名为 GACDemo。
2. 必须使用强名称。要生成此加密密钥对,请使用 SN 工具。此工具位于安装 .NET Framework 解决方案开发人员工具包 (SDK) 的 \bin 子目录中。SN 工具易于使用。命令行语句采用以下形式:
sn -k "[DriveLetter]:\[DirectoryToPlaceKey]\[KeyName].snk"
3. 在 C:\ 中创建一个名为 GACKey 的目录,以便您可以轻松地找到密钥,并从命令提示符处访问该密钥。

注意:对于大多数用户,.NET 工具位于 C:\Program Files\Microsoft.NET\FrameworkSDK\Bin 中。键入以下 SN 命令前,可能需要在您的计算机上将与该路径类似的路径复制到 .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"
如果您要拖动文件,请使用 Microsoft Windows 资源管理器。打开 Windows 资源管理器的两个实例。在一个实例中,找到控制台项目的 .dll 文件输出的位置。在另一实例中,找到 c:\[SystemRoot]\Assembly。

将您的 .dll 文件拖到“Assembly”文件夹中。

完整代码列表 (AssemblyInfo.cs)

None.gif
None.gif
using  System.Reflection;
None.gif
using  System.Runtime.CompilerServices;
None.gif
None.gif
//
None.gif
//  General Information about an assembly is controlled through the following 
None.gif
//  set of attributes. Change these attribute values to modify the information
None.gif
//  that is associated with an assembly.
None.gif
//
None.gif
[assembly: AssemblyTitle( "" )]
None.gif[assembly: AssemblyDescription(
"" )]
None.gif[assembly: AssemblyConfiguration(
"" )]
None.gif[assembly: AssemblyCompany(
"" )]
None.gif[assembly: AssemblyProduct(
"" )]
None.gif[assembly: AssemblyCopyright(
"" )]
None.gif[assembly: AssemblyTrademark(
"" )]
None.gif[assembly: AssemblyCulture(
"" )]
None.gif
None.gif
//
None.gif
//  Version information for an assembly is made up of the following four values:
None.gif
//
None.gif
//       Major Version
None.gif
//       Minor Version 
None.gif
//       Build Number
None.gif
//       Revision
None.gif
//
None.gif
//  You can specify all the values, or you can default the revision and build numbers 
None.gif
//  by using the '*' as shown below:
None.gif

None.gif[assembly: AssemblyVersion(
" 1.0.* " )]
None.gif
None.gif
//
None.gif
//  To sign your assembly you must specify a key to use. See the 
None.gif
//  Microsoft .NET Framework documentation for more information about assembly signing.
None.gif
//
None.gif
//  Use the following attributes to control that key is used for signing. 
None.gif
//
None.gif
//  Notes: 
None.gif
//    (*) If no key is specified, the assembly is not signed.
None.gif
//    (*) KeyName refers to a key that has been installed in the Crypto Service
None.gif
//        Provider (CSP) on your computer. KeyFile refers to a file that contains
None.gif
//        a key.
None.gif
//    (*) If the KeyFile and the KeyName values are both specified, the 
None.gif
//        following processing occurs:
None.gif
//        (1) If the KeyName can be found in the CSP, that key is used.
None.gif
//        (2) If the KeyName does not exist and the KeyFile does exist, the key 
None.gif
//            in the KeyFile is installed to the CSP and used.
None.gif
//    (*) To create a KeyFile, you can use the sn.exe (Strong Name) utility.
None.gif
//        When specifying the KeyFile, the location of the KeyFile must be
None.gif
//        relative to the project output directory which is
None.gif
//        %Project Directory%\obj\<configuration>. For example, if your KeyFile is
None.gif
//        located in the project directory, you would specify the AssemblyKeyFile 
None.gif
//        attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
None.gif
//    (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
None.gif
//        documentation for more information about this.
None.gif
//
None.gif
[assembly: AssemblyDelaySign( false )]
None.gif[assembly: AssemblyKeyFile(
" C:\\GACKey\\GACKey.snk " )]
None.gif[assembly: AssemblyKeyName(
"" )]
None.gif


From:
http://www.dnnchina.net/Default.aspx?tabid=455&ctl=ArticleView&mid=1153&articleId=50

    许多应用程序都使用由多个应用程序共享的程序集。这些程序集通常由第三方开发商提供,安装在系统中的公用位置——全局程序集缓存中。默认情况下,系统在全局程序集缓存中查找每一个应用程序的程序集。必须拥有管理员的权限才能将程序集安装到全局程序集缓存中。安装在全局程序集缓存中的每一个程序集都必须拥有一个强名称。通过gacutil.exe可以访问全局程序集中的缓冲对象。Gacutil –i添加,gacutil –u移除,gacutil –l查询。C:\WINDOWS\assembly 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值