【问题标题】:Error 1001. The specified service already exists. Existing service can't removed错误 1001。指定的服务已经存在。现有服务无法移除
【发布时间】:2022-01-18 03:20:13
【问题描述】:

我尝试重新安装现有服务,但它返回错误“错误 1001。指定的服务已存在”

我尝试了“sc delete servicename”,但它不起作用。对此有何意见?

【问题讨论】:

  • 您在尝试删除服务之前是否已停止服务?

标签: c# service


【解决方案1】:

在安装/升级/修复Windows服务项目时,我发现“错误1001。指定的服务已存在”的最佳解决方案是修改ProjectInstaller.Designer.cs。

将以下行添加到 InitializeComponent() 的开头,这将在您当前的安装程序再次尝试安装服务之前触发一个事件。在这种情况下,如果该服务已经存在,我们将卸载它。

一定要在cs文件的顶部添加以下内容来实现以下命名空间...

using System.Collections.Generic;
using System.ServiceProcess;

然后在示例中使用如下所示...

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

例子:

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

事件调用的以下代码将卸载该服务(如果存在)。

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

【讨论】:

  • 很好的答案,但 GetServices() 不包含 ToList() 方法,AKA ServiceController 类型不支持它。而是使用 List services = new List(ServiceController.GetServices()); - 更正添加到答案以及其他需要做的事情。
  • 我看到您已经编辑了我的答案,但 ToList() 是 Linq 的一部分并且确实有效。显然,您必须在项目中使用 System.Linq。
  • 抱歉 CathalMF,不知道。只是尝试使用提供的代码使其开箱即用。感谢您的澄清。 ;)
  • 不用担心,您的方法更简单,而且开箱即用。我可能应该指定 Linq 是必需的。
  • 谢谢。我见过的“错误 1001”问题的最佳答案。
【解决方案2】:

除了 CathalMF 回答之外,我还必须通过在代码中添加以下行来在卸载之前停止服务:

    if (s.ServiceName == this.serviceInstaller1.ServiceName)
    {
        if(s.Status == ServiceControllerStatus.Running)
           s.Stop();
        ....
    }

【讨论】:

    猜你喜欢
    • 2012-04-11
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    相关资源
    最近更新 更多