I am trying to install a windows service and start it afterwards.I do not understand why after a couple of cycles (install+uninstall) i cannot install or uninstall the service anymore since i get the error:
Another version of this product is already installed
but the service is no longer present in the Services
window , nor is the installer present in the Programs
section.
If i try to uninstall i get :
Error 1001: An exception occured while uninstalling.This exception will be ignored and the uninstall will continue.However the uninstall might not be fully uninstalled after the uininstall is complete.
I do not understand what i am doing wrong. I have added my project output to all custom actions:
-Install
-Uninstall
-Commit
-Rollback
How is one supposed to perform clean installs/uninstalls ?
Installer Code
[RunInstaller(true)]
public partial class ProjectInstaller : Installer {
public ProjectInstaller() {
InitializeComponent();
this.AfterInstall += ProjectInstaller_AfterInstall;
this.BeforeUninstall += ProjectInstaller_BeforeUninstall;
}
private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) {
StartService();
}
private void ProjectInstaller_BeforeUninstall(object sender, InstallEventArgs e) {
StopService();
}
private void StartService() {
Debugger.Launch();
bool isAdmin = IsAdmin();
if (isAdmin) {
using (ServiceController controller = new ServiceController(serviceInstaller1.ServiceName)) {
controller.Start();
}
} else {
ProcessStartInfo info = new ProcessStartInfo {
Verb = "runas",
FileName = "net",
Arguments = $"start {serviceInstaller1.ServiceName}"
};
Process.Start(info);
}
}
private void StopService() {
bool isAdmin = IsAdmin();
if (isAdmin) {
using (ServiceController controller = new ServiceController(serviceInstaller1.ServiceName)) {
controller.Stop();
}
} else {
ProcessStartInfo info = new ProcessStartInfo {
Verb = "runas",
FileName = "net",
Arguments = $"stop {serviceInstaller1.ServiceName}"
};
Process.Start(info);
}
}
private static bool IsAdmin() {
var identity = WindowsIdentity.GetCurrent();
var princ = new WindowsPrincipal(identity);
return princ.IsInRole(WindowsBuiltInRole.Administrator);
}
}