有什么方法可以在不重新编译的情况下覆盖 .NET Windows 服务名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1704554/
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
Any way to override .NET Windows Service Name without recompiling?
提问by Nathan
I have a windows service executable that I know is written in .NET which I need to install under a different service name to avoid a conflict. The install doesn't provide anyway to specify a service name. If I only have access to the binary, is there anyway to override the service name when I install it with installutil?
我有一个 Windows 服务可执行文件,我知道它是用 .NET 编写的,我需要在不同的服务名称下安装它以避免冲突。无论如何,安装不提供指定服务名称。如果我只能访问二进制文件,那么在我使用 installutil 安装它时是否可以覆盖服务名称?
回答by Josh Yeager
Do you have to use InstallUtil? Here are the commands to do what you want using sc:
你必须使用InstallUtil吗?以下是使用 sc 执行所需操作的命令:
sc create MyService binPath= "MyService.exe" DisplayName= "MyService"
sc description MyService "My description"
Reference: http://support.microsoft.com/kb/251192
回答by Sachin Kainth
It is not true that InstallUtil doesn't allow you to configure the service name. I do it all the time like this
InstallUtil 不允许您配置服务名称是不正确的。我一直这样做
InstallUtil.exe /servicename="<service name>" "<path to service exe>"
回答by Volodymyr Bilyachat
- Add project installer to your service
Add method to get CustomService name
private void RetrieveServiceName() { var serviceName = Context.Parameters["servicename"]; if (!string.IsNullOrEmpty(serviceName)) { this.SomeService.ServiceName = serviceName; this.SomeService.DisplayName = serviceName; } }call on install and uninstall
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
- 将项目安装程序添加到您的服务
添加获取自定义服务名称的方法
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
回答by kulNinja


This exactly worked for me!
这对我来说完全有用!
I hope someone can use this.
我希望有人可以使用这个。
回答by jsw
Try installing your service with sc.exe. A quick search will yield lots documentation. With that tool it's easy to modify existing services and/or add new ones -- including names.
尝试使用 sc.exe 安装您的服务。快速搜索将产生大量文档。使用该工具可以轻松修改现有服务和/或添加新服务——包括名称。
Edit: I install my .NET services with this tool.
编辑:我用这个工具安装了我的 .NET 服务。

