1

I have a project installer which installs a Windows service.
When I create a new version I need to uninstall it and install the new version.
Please check the code below:

private void InitializeComponent()
{
    this.ServiceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
    this.MyServiceInstaller = new System.ServiceProcess.ServiceInstaller();
    // 
    // ServiceProcessInstaller
    // 
    this.ServiceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
    this.ServiceProcessInstaller.Password = null;
    this.ServiceProcessInstaller.Username = null;
    this.ServiceProcessInstaller.BeforeInstall += new System.Configuration.Install.InstallEventHandler(this.BeforeInstall);
    // 
    // MyServiceInstaller
    // 
    this.MyServiceInstaller.Description = "Description";
    this.MyServiceInstaller.ServiceName = "My Service";
    // 
    // ProjectInstaller
    // 
    this.Installers.AddRange(new System.Configuration.Install.Installer[] {
    this.ServiceProcessInstaller,
    this.MyServiceInstaller});

}


void BeforeInstall(object sender, System.Configuration.Install.InstallEventArgs e)
{
    List<ServiceController> services = new List<ServiceController>(ServiceController.GetServices());

    foreach (ServiceController s in services)
    {
        if (s.ServiceName == "My Service")
        {
            ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
            ServiceInstallerObj.Context = new InstallContext();
            ServiceInstallerObj.ServiceName = "My Service";
            ServiceInstallerObj.Uninstall(null);

            break;
        }
    }
}

I set the BeforeInstall event but I got Error 1001 again.


Hp_issei
  • 579
  • 6
  • 18
enfix
  • 6,680
  • 12
  • 55
  • 80
  • Heading out, here are some links: [How to install a service using WiX](https://github.com/Robs79/How-to-create-a-Windows-Service-MSI-Installer-Using-WiX). And then there is [this answer on how to deploy services using different MSI Tools](https://stackoverflow.com/questions/50225031/windows-service-not-shown-in-add-remove-programs-under-control-panel/50229840#50229840). – Stein Åsmul Nov 08 '19 at 18:03

0 Answers0