服务器无响应1001,错误1001:指定的服务已存在。 无法删除现有的服务

**如果需要使用设置完成,请按照下列步骤操作:

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

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

添加以下命名空间:

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 services = new List(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:随着上述变化,请考虑更新设置版本,产品代码(和可选的升级代码),以便更好的实践,更好的版本管理,跟踪和维护

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值