【发布时间】:2010-12-14 20:13:53
【问题描述】:
我有一个 Windows 服务可执行文件,我知道它是用 .NET 编写的,我需要以不同的服务名称安装它以避免冲突。安装无论如何都不提供指定服务名称。如果我只能访问二进制文件,那么当我使用 installutil 安装它时是否可以覆盖服务名称?
【问题讨论】:
标签: .net windows-services
我有一个 Windows 服务可执行文件,我知道它是用 .NET 编写的,我需要以不同的服务名称安装它以避免冲突。安装无论如何都不提供指定服务名称。如果我只能访问二进制文件,那么当我使用 installutil 安装它时是否可以覆盖服务名称?
【问题讨论】:
标签: .net windows-services
您必须使用 InstallUtil 吗?以下是使用 sc 执行所需操作的命令:
sc create MyService binPath= "MyService.exe" DisplayName= "MyService"
sc description MyService "My description"
【讨论】:
InstallUtil 不允许您配置服务名称是不正确的。我一直都是这样的
InstallUtil.exe /servicename="<service name>" "<path to service exe>"
【讨论】:
System.ComponentModel.Win32Exception: The specified service already exists。我试图安装 2 个相同服务的实例并以不同的方式命名它们。使用以下答案中给出的 sc create 方法
添加获取自定义服务名称的方法
private void RetrieveServiceName()
{
var serviceName = Context.Parameters["servicename"];
if (!string.IsNullOrEmpty(serviceName))
{
this.SomeService.ServiceName = serviceName;
this.SomeService.DisplayName = serviceName;
}
}
调用安装和卸载
public override void Install(System.Collections.IDictionary stateSaver)
{
RetrieveServiceName();
base.Install(stateSaver);
}
public override void Uninstall(System.Collections.IDictionary savedState)
{
RetrieveServiceName();
base.Uninstall(savedState);
}
installutil /servicename=”My Service [SysTest]” d:\pathToMyService\Service.exe
【讨论】:
这对我很有效!
我希望有人可以使用它。
【讨论】:
尝试使用 sc.exe 安装您的服务。快速搜索将产生大量文档。使用该工具可以轻松修改现有服务和/或添加新服务——包括名称。
编辑:我用这个工具安装我的 .NET 服务。
【讨论】: