C# 错误 1001:指定的服务已经存在。无法删除现有服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9895586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:07:20  来源:igfitidea点击:

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

c#windowsvisual-studio-2008windows-services

提问by Larry Gasik

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.

我有一个服务。我不久前安装了它。我需要更新服务。我去了添加/删除程序并寻找我的服务,但它没有安装在那里。我查看了 services.msc,它就在那里,停了下来。我能够启动它并停止它。我以管理员身份运行命令提示符并运行 sc delete [服务名称],并收到“指定的服务不作为已安装的服务存在”。我在命令提示符下做了一个 sc 查询,它没有返回。我右键单击安装程序,单击卸载并收到“此操作仅对当前安装的产品有效”。我也尝试修复,并得到相同的消息。

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.

我已经重新启动了几次机器,但没有成功卸载此服务。我使用的是随 Visual Studio 一起安装的基本安装项目模板。我试过更改程序的名称,并增加版本号。

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

如何卸载显然存在的服务,并防止将来发生这种情况?

回答by NicoRiff

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

您是否尝试在 Windows 注册表中查找与该服务相关的垃圾?

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

您应该查看此文件夹:HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \

回答by Mario The Spoon

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

如果您有包含服务安装程序的 .exe,请使用InstallUtil.exe /u <process.exe>InstallUtil.exe 位于\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

马里奥

回答by Jirka Hanika

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

使用sc delete是正确的。您需要在双引号中包含服务的(短)名称(除非它只是一个单词),但仅此而已。

Failing that, visit HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servicesin your registry, both 32 bit and 64 bit (regedt32.exeand 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 scdoes 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).

如果失败,请访问HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services您的注册表,包括 32 位和 64 位(分别为regedt32.exeregedit.exe)。您甚至可以直接删除那里的服务,但显然您应该从可逆更改开始,以诊断您的服务是如何准确命名的,以及为什么sc看不到它的名称,并且只有在其他一切都失败并备份了您的服务后才使用直接注册表访问注册表(谷歌这个程序,指定您的操作系统)。

回答by tanzer

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.

今天我也发生了同样的事情。唯一的解决方案是从 Windows 添加/删除工具修复安装文件。修复安装文件后,卸载并重新安装。

回答by 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:

这可以通过显式实现现有服务删除(卸载)然后允许安装更新版本来处理。为此,我们需要更新 ProjectInstaller.Designer.cs 如下:

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.

考虑在 InitializeComponent() 的开头添加以下行,它会在当前安装程序尝试再次安装服务之前触发卸载现有服务的事件。如果服务已经存在,我们在这里卸载它。

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

PS:随着上述变化,也请考虑更新安装版本、产品代码(和可选的升级代码)以获得良好的实践、更好的版本管理、跟踪和维护

回答by Ryan Den-Kaat

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.

对我有用的是更新我的安装程序的名称、版本和产品代码。无论如何,绝对应该遵循版本控制的良好做法。