For creating an Installer for a Windows Service, .NET has a nice standard control which you just add using the New File Wizard.
However, uninstalling a Windows Service needs a bit more work. If the Service is not unregistered, then subsequent installs will fail.
So here is a bit of code which runs "InstallUtil.exe /u" to unregister the service.
To enable the code, you need to:
1) Add a new custom action to the Setup project, for 'Before Uninstall'
2) Use the Designer to handle the Before Uninstall event in your derivation of Installer class.
The code you need to write, is in blue below:
public partial class Installer1 : Installer
{
public ProjectInstaller()
{
InitializeComponent();
this.BeforeUninstall += new InstallEventHandler(ProjectInstaller_BeforeUninstall);
}
private void serviceInstaller1_BeforeUninstall(object sender, InstallEventArgs e)
{
//try to unregister the service, using "InstallUtil.exe /u"
string[] myArgs = Environment.GetCommandLineArgs();
string[] installUtilArgs = new string[2]; //new string[myargs->Length - 1];
installUtilArgs[0] = "/u";
installUtilArgs[1] = (installUtilArgs[0]);
//Array.Copy(myargs, 2, installUtilArgs, 1, installUtilArgs.Length - 1);
AppDomain dom = AppDomain.CreateDomain("execDom");
Type type = typeof(object);
string path = type.Assembly.Location;
StringBuilder sb = new StringBuilder(path.Substring(0, path.LastIndexOf("\\")));
sb.Append("\\InstallUtil.exe");
dom.ExecuteAssembly(sb.ToString(), null, installUtilArgs); }
}
However, uninstalling a Windows Service needs a bit more work. If the Service is not unregistered, then subsequent installs will fail.
So here is a bit of code which runs "InstallUtil.exe /u" to unregister the service.
To enable the code, you need to:
1) Add a new custom action to the Setup project, for 'Before Uninstall'
2) Use the Designer to handle the Before Uninstall event in your derivation of Installer class.
The code you need to write, is in blue below:
public partial class Installer1 : Installer
{
public ProjectInstaller()
{
InitializeComponent();
this.BeforeUninstall += new InstallEventHandler(ProjectInstaller_BeforeUninstall);
}
private void serviceInstaller1_BeforeUninstall(object sender, InstallEventArgs e)
{
//try to unregister the service, using "InstallUtil.exe /u"
string[] myArgs = Environment.GetCommandLineArgs();
string[] installUtilArgs = new string[2]; //new string[myargs->Length - 1];
installUtilArgs[0] = "/u";
installUtilArgs[1] = (installUtilArgs[0]);
//Array.Copy(myargs, 2, installUtilArgs, 1, installUtilArgs.Length - 1);
AppDomain dom = AppDomain.CreateDomain("execDom");
Type type = typeof(object);
string path = type.Assembly.Location;
StringBuilder sb = new StringBuilder(path.Substring(0, path.LastIndexOf("\\")));
sb.Append("\\InstallUtil.exe");
dom.ExecuteAssembly(sb.ToString(), null, installUtilArgs); }
}
Comments
Post a Comment