我有服务。我刚刚安装了它。我需要对服务进行更新。我去了添加/删除程序并查找我的服务,它没有安装在那里。我查看了services.msc,它就在那里,停了下来。我能够启动它并阻止它。我以管理员身份运行命令提示符并运行sc delete [Service Name],并收到“指定的服务不作为已安装的服务存在”。我在命令提示符下执行了sc查询,但未返回。我右键单击安装程序,单击卸载并收到“此操作仅对当前安装的产品有效”。我也试过修理,并得到了同样的信息。
我已经重新启动了几次机器,没有运气让这个服务卸载。我正在使用随Visual Studio安装的基本安装项目模板。我已经尝试更改程序的名称,并增加版本号。
如何卸载显然存在的服务,并防止将来发生这种情况?
如果您有包含服务安装程序使用的.exe InstallUtil.exe /u <process.exe>
找到InstallUtil.exe \Windows\Microsoft.Net\Framework\v4.0.30319
在安装项目中,将您的服务包含在所有自定义操作中,也包括卸载
(右键单击项目,自定义操作)
心连心
马里奥
**如果只需要使用设置完成,请按照:
这可以通过显式实现现有服务删除(卸载),然后允许安装更新版本来处理。
为此,我们需要更新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<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:除上述更改外,还请考虑更新安装版本,ProductCode(和选项版本UpgradeCode)以获得良好实践,更好的版本管理,跟踪和维护
在“添加/删除程序”中未列出该服务是完全正常的,该列表适用于软件包,而不是服务。 (一个程序包或程序可能包含多个服务,但通常不会安装任何服务。)
显然,该服务是手动安装的,而不是作为产品的一部分安装,即使特别是这个服务通常会安装一个产品的安装包。
运用 sc delete
是正确的。您需要在双引号中包含服务的(简短)名称(除非它只是一个单词),但没有别的。
如果做不到,请访问 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
在您的注册表中,32位和64位(regedt32.exe
和 regedit.exe
, 分别)。您甚至可以直接删除那里的服务,但显然应该从可逆的更改开始,以诊断您的服务的确切命名方式和原因 sc
没有看到它的名字,只有在其他所有方法都失败后才能使用直接注册表访问权限以及备份注册表之后(谷歌此程序指定您的操作系统)。
我今天也发生了同样的事。唯一的解决方案是从Windows添加/删除工具修复安装文件。修复安装文件后,卸载并重新安装。
您是否尝试在Windows注册表中查找与该服务相关的垃圾?
您应该查看此文件夹:HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \
以防其他人遇到此问题:
对我有用的是更新我的安装程序的名称,版本和ProductCode。绝对应该遵循版本控制的良好实践。