Search code examples
c#windows-serviceswindows-installeruninstallation

Solving Another version of program is already installed exception


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);
        }
}

Solution

  • TEMP: Adding this answer, not sure it is relevant until we hear more comments from OP.


    Custom Actions: MSI has built-in mechanisms to start / stop and install / uninstall services that are quite reliable. These involve populating a number of standard MSI tables. Using custom actions could trigger problems like you describe, that are hard to debug and solve.


    Deployment Tools: What tool are you using?


    WiX Toolset: WiX is a free, Open Source alternative, and there are several other major deployment tools you can check. Advanced Installer has some free features, but I don't think that includes service installation. Worth a test though - nice features. Installshield has no free version that I know of, but is full-featured. PACE suite is the new kid on the block. I would test them all and pick one - just 2 cents.

    WiX: Service Installation Samples: