Any way to override .NET Windows Service Name without recompiling?

假装没事ソ 提交于 2019-11-29 19:15:27
Josh Yeager

Do you have to use InstallUtil? Here are the commands to do what you want using sc:

sc create MyService binPath= "MyService.exe" DisplayName= "MyService"  
sc description MyService "My description"

Reference: http://support.microsoft.com/kb/251192

It is not true that InstallUtil doesn't allow you to configure the service name. I do it all the time like this

InstallUtil.exe /servicename="<service name>" "<path to service exe>"
Volodymyr Bilyachat
  1. Add project installer to your service
  2. 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;
        }
    }
    
  3. 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);
    }
    
  4. installutil /servicename=”My Service [SysTest]” d:\pathToMyService\Service.exe

Source

This exactly worked for me!

I hope someone can use this.

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.

Edit: I install my .NET services with this tool.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!