如何使用Visual Studio 2008部署.dll文件?

3
我正在使用VS 2008创建一个安装程序,其中包含一个桌面应用程序和一个作为类库项目创建的BHO插件。 手动地,我可以使用这个命令[regasm /codebase "myPlugin.dll" register] 注册 myPlugin.dll ,但我不知道如何在设置项目中实现这一点。有什么建议吗?
1个回答

2
最简单且安全的方法是创建自己的安装程序类,该类接受要注册的 DLL 的名称,并使用 RegistrationServices 安装或卸载您的 DLL。
当您创建一个 Installer 时,它可以通过自定义操作的 CustomActionData 属性接受参数。每个参数都存储在 Installer.Context.Parameters 集合中。这意味着通过使用一些安装程序属性,您可以传递完整的已安装 DLL 路径,然后 RegistrationServices 可以执行与 Regasm 相同的操作。
实现这个过程需要多个步骤:
第一步是创建安装程序类。因为这是可重用的代码,建议创建一个单独的 DLL 项目来保存此类。
using System;
using System.Text;
using System.ComponentModel;
using System.Configuration.Install;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

[RunInstaller(true)]
public partial class AssemblyRegistrationInstaller : Installer
{
    public AssemblyRegistrationInstaller()
    {
    }

    public override void Install(IDictionary stateSaver)
    {
        base.Install(stateSaver);

        RegisterAssembly(true);
    }

    public override void Rollback(IDictionary savedState)
    {
        base.Rollback(savedState);

        RegisterAssembly(false);
    }

    public override void Uninstall(IDictionary savedState)
    {
        base.Rollback(savedState);

        RegisterAssembly(false);
    }

    private void RegisterAssembly(bool fDoRegistration)
    {
        string sAssemblyFileName = base.Context.Parameters["AssemblyFileName"];

        if (string.IsNullOrEmpty(sAssemblyFileName))
        {
            throw new InstallException("AssemblyFileName must be specified");
        }

        if (!File.Exists(sAssemblyFileName))
        {
            if (fDoRegistration)
            {
                throw new InstallException(string.Format("AssemblyFileName {0} is missing", sAssemblyFileName));
            }
            else
            {
                // Just bail if we are uninstalling and the file doesn't exist
                return;
            }
        }

        var oServices = new RegistrationServices();
        var oAssembly = Assembly.LoadFrom(sAssemblyFileName);

        if (fDoRegistration)
        {
            oServices.RegisterAssembly(oAssembly, AssemblyRegistrationFlags.SetCodeBase);
        }
        else
        {
            try
            {
                oServices.UnregisterAssembly(oAssembly);
            }
            catch
            {
                // Just eat the exception so that uninstall succeeds even if there is an error
            }
        }
    }
}

步骤2是将新的DLL添加到安装程序项目中。由于我们需要为DLL配置设置,因此不要添加项目本身;直接从项目的发布目录添加DLL。
步骤3是将新的DLL添加为自定义操作。为此,请右键单击安装程序项目,然后选择“查看”,再选择“自定义操作”。
右键单击“自定义操作”菜单,在弹出的对话框中选择新的安装程序DLL。这将自动将DLL添加到所有4个部分。右键单击添加到“提交”部分的项并将其删除。
对于其他3个部分,执行以下操作:
右键单击DLL条目,然后选择“属性窗口”。
确保属性网格中的“InstallerClass”条目设置为true(应该是)。
将以下条目添加到“CustomActionData”属性中,以将要注册的DLL名称传递给自定义安装程序类:
/AssemblyFileName="[TARGETDIR]\myplugin.dll"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接