Error 1001: The Specified Service Already Exists. Cannot remove existing service

非 Y 不嫁゛ 提交于 2019-12-03 11:04:41

If you have the .exe that includes the service installer use InstallUtil.exe /u <process.exe> InstallUtil.exe is found in \Windows\Microsoft.Net\Framework\v4.0.30319

In the setup project, include your service in all custom actions, also the uninstall

(right click on the project, Custom Action)

hth

Mario

ShivanandSK

** If it is required to be done using the setup only, please follow:

This can be handled by explicit implementation of existing service removal (uninstall) and then allowing newer version to install. For this, we need to update ProjectInstaller.Designer.cs as below:

Consider adding following line at the beginning of InitializeComponent() which triggers an event for uninstalling the existing service before your current installer tries to install the service again. Here we uninstall the service if it already exists.

Add following namespaces:

using System.Collections.Generic;
using System.ServiceProcess;
using System.Configuration.Install;

Add below line of code as described before:

this.BeforeInstall += new
System.Configuration.Install.InstallEventHandler(ProjectInstaller_BeforeInstall);

Example:

private void InitializeComponent()
{
    this.BeforeInstall += new System.Configuration.Install.InstallEventHandler(ProjectInstaller_BeforeInstall);

    this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
    this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
    // 
    // serviceProcessInstaller1
    // 
    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
    this.serviceProcessInstaller1.Password = null;
    this.serviceProcessInstaller1.Username = null;
    // 
    // serviceInstaller1
    // 
    this.serviceInstaller1.Description = "This is my service name description";
    this.serviceInstaller1.ServiceName = "MyServiceName";
    this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
    // 
    // ProjectInstaller
    // 
    this.Installers.AddRange(new System.Configuration.Install.Installer[]{
            this.serviceProcessInstaller1,
            this.serviceInstaller1
        }
    );
}

The below code called by the event will then uninstall the service if it exists.

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

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

            break;
        }
    }
}

PS: Along with the above changes, also please consider updating the setup Version, ProductCode (, and optionall UpgradeCode) for good practice, better version management, tracking and maintenance

It is completely normal that the service is not listed in Add/Remove Programs, that listing is for software packages, not services. (One package, or program, may contain multiple services, but it typically installs none.)

Apparently, the service was installed manually, not as part of the product, even if this one in particular would normally install with a product whose installation package you have got.

Using sc delete is correct. You will need to include the (short) name of the service in double quotes (unless it is just a single word), but nothing else.

Failing that, visit HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services in your registry, both 32 bit and 64 bit (regedt32.exe and regedit.exe, respectively). You can even delete the service there directly, but you should obviously start by reversible changes to diagnose how is your service exactly named and why sc does not see its name and only use direct registry access after everything else has failed and after you have backed up your registry (google this procedure up specifying your operating system).

Have you tried looking in the Windows Registry for some trash relating to that service?.

You should look on this folder: HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \

Same happened to me today. The only solution was to repair the setup file from Windows Add/Remove tool. After repairing your setup file uninstall and install it again.

Just in case anyone else comes across this issue:

What worked for me was updating the Name, Version and ProductCode of my Installer. Should definitely follow good practice of versioning anyways.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!