我正在使用VS 2008创建一个安装程序,其中包含一个桌面应用程序和一个作为类库项目创建的BHO插件。
手动地,我可以使用这个命令[regasm /codebase "myPlugin.dll" register] 注册 myPlugin.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
}
}
}
}
/AssemblyFileName="[TARGETDIR]\myplugin.dll"