zoukankan      html  css  js  c++  java
  • 批文件注册程序集 Timothy

     

    1. 新建一个解决方案,命名为GAC。

    2. 添加一个Class Library类库,命名为DbConn。

    using System;

    using System.Collections.Generic;

    using System.Text;

     

    namespace DbConn

    {

        public class Class1

        {

            public string GetCustomer()

            {

                return "Hello World!";

            }

        }

    }

    3. 在解决方案中继续添加一个Windows Form应用程序,命名为HellpApp。

    添加DbConn的引用:右击引用->添加引用->项目,选中DbConn然后点击确定添加引用到Windows Form中。

    添加一个按钮到Form1中,点击按钮调用Class1中的GetCustomer()方法,Form1.Designer.cs代码如下。

    namespace HellpApp

    {

        partial class Form1

        {

            /// <summary>

            /// 必需的设计器变量。

            /// </summary>

            private System.ComponentModel.IContainer components = null;

     

            /// <summary>

            /// 清理所有正在使用的资源。

            /// </summary>

            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

            protected override void Dispose(bool disposing)

            {

                if (disposing && (components != null))

                {

                    components.Dispose();

                }

                base.Dispose(disposing);

            }

     

            #region Windows 窗体设计器生成的代码

     

            /// <summary>

            /// 设计器支持所需的方法 - 不要

            /// 使用代码编辑器修改此方法的内容。

            /// </summary>

            private void InitializeComponent()

            {

                this.button1 = new System.Windows.Forms.Button();

                this.SuspendLayout();

                //

                // button1

                //

                this.button1.Location = new System.Drawing.Point(66, 89);

                this.button1.Name = "button1";

                this.button1.Size = new System.Drawing.Size(75, 23);

                this.button1.TabIndex = 0;

                this.button1.Text = "Click";

                this.button1.UseVisualStyleBackColor = true;

                this.button1.Click += new System.EventHandler(this.button1_Click);

                //

                // Form1

                //

                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

                this.ClientSize = new System.Drawing.Size(292, 266);

                this.Controls.Add(this.button1);

                this.Name = "Form1";

                this.Text = "Form1";

                this.ResumeLayout(false);

     

            }

            #endregion

     

            private System.Windows.Forms.Button button1;

        }

    }

     

    按钮点击事件代码如下,Form1.cs。

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using DbConn;

     

    namespace HellpApp

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                Class1 cl = new Class1();//Class1存在于另一个解决方案中

                string str=cl.GetCustomer();

                MessageBox.Show(str);

            }

        }

    }

    4. 编译以后或者按F5运行应用程序,会把HellpApp中引用的DbConn程序集复制到HellpApp目录中,运行时也就不行出现找不到程序集的错误。但是对于异步引用程序,可能有多个exe文件要访问DbConn程序集,为了实现程序集的重复部署和避免漏更新错误,往往要把程序集注册到注册表中,本机中的所有引用程序都可以访问即使他们不再同一个目录下。

    5. 首先,要给待部署的程序集强命名。

    Microsoft Visual Studio 2005->Visual Studio Tools->启用Visual Studio 2005命令提示。

    然后,cd E:"Code"GAC"DbConn进入Class1.cs目录中。我的机器是保存在E盘,可以根据情况自己调整。

    然后,sn –k DbConn.snk命令可以生成强命名文件。

    打开DbConn项目的AssemblyInfo.cs文件,并添加强命名文件到项目配置文件中。

    using System.Reflection;

    using System.Runtime.CompilerServices;

    using System.Runtime.InteropServices;

     

    // 有关程序集的常规信息通过下列属性集

    // 控制。更改这些属性值可修改

    // 与程序集关联的信息。

    [assembly: AssemblyTitle("DbConn")]

    [assembly: AssemblyDescription("")]

    [assembly: AssemblyConfiguration("")]

    [assembly: AssemblyCompany("")]

    [assembly: AssemblyProduct("DbConn")]

    [assembly: AssemblyCopyright("版权所有 (C) 2008")]

    [assembly: AssemblyTrademark("")]

    [assembly: AssemblyCulture("")]

     

    // 将 ComVisible 设置为 false 使此程序集中的类型

    // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,

    // 则将该类型上的 ComVisible 属性设置为 true。

    [assembly: ComVisible(false)]

     

    // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID

    [assembly: Guid("19a9e567-b3b0-4d65-b997-1c2cebc36674")]

     

    // 程序集的版本信息由下面四个值组成:

    //

    //      主版本

    //      次版本

    //      内部版本号

    //      修订号

    //

    // 可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,

    // 方法是按如下所示使用“*”:

    [assembly: AssemblyVersion("1.0.0.1")]

    [assembly: AssemblyFileVersion("1.0.0.1")]

    [assembly: AssemblyKeyFile("DbConn.snk")]

    重新编译DbConn项目文件,这个时候生成的dll文件是被强命名的,强名主要是为了防止多个公司的程序集同名问题。

    6. 批文件注册程序集到GAC(Global Assembly Cache)中。

    找到gacutil.exe文件,配置有.net环境的机器会默认安装。一般情况下,放在C:"Program Files"Microsoft Visual Studio 8"SDK"v2.0"Bin目录中。

    复制一份gacutil.exe和gacutil.exe.config 文件到DbConn.dll目录中,也可以新建一个文件,但要保持他们在同一目录中。

    新建一个文本文件到DbConn.dll目录中,并重命名为Reg.bat,右击Edit,批处理代码如下。

    @ECHO Preparing for the registration

    gacutil -u DbConn –如果存在同名,先卸载

     

    gacutil -i DbConn.dll—安装程序集

     

    @ECHO Registration process finished

    Pause

    7. 双击Reg.bat文件,程序集会被自动安装到GAC中。

    8. 点击运行->输入assembly,点击确定会打开本机中注册的所有程序集,你会发现DbConn已经被注册了。

    9. 这个时候,即使HellpApp和DbConn不在同一目录下,也可以成功的访问。所以,对于异步架构的系统很有帮助,可以解决一次部署,全部更新的作用。

    10. 如果想要把部署工作做的更完善,也可以新建一个应用程序,点击按钮等操作来完成程序集的注册。

    可以在直接用System.Diagnostics.Process.Start("batFilePath"Reg.bat")直接调用写好的批处理程序。

     

     

    附录:

    Difference between regasm.exe, regsvcs.exe and gacutil.exe
    1.gacutil.exe
    used for putting shared assembly in GAC( global Assembly Cache- Place for shared assemblies to share among applications)


    2.regsvcs.exe
    Generates, registers, and installs a type library into a specified COM+ 1.0 application.

    3.regasm.exe.
    For making dotnet assemblies to interact with any com objects.

    Hope this gives you a bird-view of the same..
    Since you are new to .NET,I think u don't need to study all the tools provided by the dotnet SDK.

     

     

    al.exe程序集链接

    如果该DLL是托管的,你可以把它解析为IL(ildasm   /source   test.dll   /out:test.il   )

     

  • 相关阅读:
    String与StringBuffer
    oracleSQL文
    中国IT成功人士特点6大成功密码全解析
    对java学习有帮助
    Spring之工厂模式
    搭建IBatis 框架
    单例模式(Singleton)
    UVa 10180 Rope Crisis in Ropeland!
    HDU 3711 Binary Number
    UVaLive 4643 / LA 4643 Twenty Questions(对题意的解释已修改)
  • 原文地址:https://www.cnblogs.com/haiyang1985/p/1375472.html
Copyright © 2011-2022 走看看