65.9K
CodeProject 正在变化。 阅读更多。
Home

使用安装项目将自定义工具部署到开发人员计算机

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (3投票s)

2009年4月8日

CPOL

1分钟阅读

viewsIcon

22260

downloadIcon

301

让自定义工具在开发人员机器上工作需要执行几个操作,例如将键值放入注册表并在regasm命令中注册您的DLL库。本文将讨论使用Setup Project自动化这些过程。

引言

让自定义工具在开发人员机器上工作需要执行几个操作,例如将键值放入注册表并在regasm命令中注册您的DLL库。本文将讨论使用Setup Project自动化这些过程。有关创建您自己的自定义工具的更多信息,请访问我的文章 为Visual Studio构建自定义工具生成器

Using the Code

首先,在您的CustomTool 库项目中添加一个继承自Installer类的类。在该类中,重写两个方法;第一个是Install方法,第二个是Uninstall方法。要获取regasm.exe的路径,可以使用InteropServices.RuntimeEnvironment类来获取.NET Framework的运行时目录。然后您需要获取程序集的位置。之后,您可以简单地启动regasm进程,并向其传递/codebase 参数和库的路径。

public override void Install(System.Collections.IDictionary stateSaver)
{
    base.Install(stateSaver);
    string regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.
					GetRuntimeDirectory() + @"regasm.exe";
    string componentPath = base.GetType().Assembly.Location;
    System.Diagnostics.Process.Start(regasmPath, "/codebase \"" + componentPath + "\"");
}

要卸载,使用/unregister 参数删除组件注册。

public override void Uninstall(System.Collections.IDictionary savedState)
{
    base.Uninstall(savedState);
    string regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.
					GetRuntimeDirectory() + @"regasm.exe";
    string componentPath = base.GetType().Assembly.Location;
    System.Diagnostics.Process.Start
		(regasmPath, "/unregister \"" + componentPath + "\"");
}

Setup Project

现在创建一个新的Setup Project并将CustomTool 库的输出添加到其中。然后右键单击Setup Project,选择“查看”,然后选择“自定义操作”。

customactions2

然后将CustomTool 项目输出添加到“安装”和“卸载”部分,如图所示

customtoolsetup

好的,现在您需要添加注册表值。右键单击Setup Project,然后选择“查看”,然后选择“注册表”。要添加所需的注册表值,请按照下图中的结构进行操作

customtoolregistry

分享您的想法

现在您所要做的就是构建Setup Project,您将获得自定义工具的Setup部署。请分享您的想法,并随时发表评论。希望您喜欢这个项目。

© . All rights reserved.