Larry Gasik
Larry Gasik

Reputation: 585

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

I have a service. I installed it a while ago. I need to do an update to the service. I went to the Add/Remove Programs and looked for my service, and it is not installed there. I looked at services.msc and it is there, stopped. I was able to start it and stop it. I ran a command prompt as administrator and ran sc delete [Service Name], and recieved "The specified service does not exist as an installed service." I did a sc query in the command prompt, and it is not returned. I right clicked on the installer, clicked on uninstall and recieved "This action is only valid for products that are currently installed." I tried repair as well, and got the same message.

I have restarted the machine a few times, and no luck getting this service to uninstall. I'm using the basic Setup Project template installed with Visual Studio. I've tried changing the name of the program, and increasing the version number.

How do I uninstall the service that apparently exists, and prevent this happening in the future?

Upvotes: 14

Views: 39918

Answers (6)

ShivanandSK
ShivanandSK

Reputation: 659

** 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;

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 System.Configuration.Install.InstallContext();
            ServiceInstallerObj.Context = Context;
            ServiceInstallerObj.ServiceName = "MyServiceName";
            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

Upvotes: 11

Ryan Den-Kaat
Ryan Den-Kaat

Reputation: 118

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.

Upvotes: 0

Mario The Spoon
Mario The Spoon

Reputation: 4859

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

Upvotes: 9

tanzer
tanzer

Reputation: 302

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.

Upvotes: 1

Jirka Hanika
Jirka Hanika

Reputation: 13529

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).

Upvotes: 5

NicoRiff
NicoRiff

Reputation: 4883

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 \

Upvotes: 0

Related Questions