2009-11-09 86 views
52

我有一个windows服务可执行文件,我知道是用.NET编写的,我需要在不同的服务名称下安装,以避免冲突。安装不提供指定服务名称。如果我只能访问二进制文件,那么在使用installutil进行安装时,是否有覆盖服务名称的方法?任何方式来重写.NET Windows服务名称而不重新编译?

回答

84

您是否必须使用InstallUtil?下面是命令做你想要使用的是什么SC:

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

参考:http://support.microsoft.com/kb/251192

+0

这看起来正是我想要的 - 但我无法让它工作。我只是不断收到“使用”消息。 – Nathan 2009-11-10 15:42:17

+30

我的问题是显然必须是等号和binPath值之间的空格,例如, sc创建ahSchedulerService binPath =“MyService.exe”,而不是sc创建ahSchedulerService binPath =“MyService.exe”。 – Nathan 2009-11-10 15:56:15

+0

啊,我忘记了。对不起,给你一个不好的例子。即使使用管理员权限cmd, – 2009-11-11 19:47:39

1

尝试使用sc.exe安装服务。快速搜索将产生大量文档。使用该工具,修改现有服务和/或添加新服务(包括名称)很容易。

编辑:我用这个工具安装我的.NET服务。

22

这是不正确的InstallUtil不允许您配置的服务名称。我做这一切的时候像这样

InstallUtil.exe /servicename="<service name>" "<path to service exe>" 
+1

也不适用于我。 – 2014-05-08 17:23:54

+4

如果你已经有一个与exe同名的服务,它会给出错误'System.ComponentModel.Win32Exception:指定的服务已经存在'。我试图安装2个相同服务的实例并以不同的方式命名它们。 使用sc创建方法在下面的答案中给出 – PUG 2014-12-15 22:50:52

+3

不起作用。给出我的服务已经存在的错误。 – Loophole 2015-09-01 19:42:18

3

enter image description here

这正好为我工作!

我希望有人可以使用它。

20
  1. 添加项目安装到您的服务
  2. Add方法来获取CustomService名称上安装

    private void RetrieveServiceName() 
    { 
        var serviceName = Context.Parameters["servicename"]; 
        if (!string.IsNullOrEmpty(serviceName)) 
        { 
         this.SomeService.ServiceName = serviceName; 
         this.SomeService.DisplayName = serviceName; 
        } 
    } 
    
  3. 通话和卸载

    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

+0

非常有帮助 - 谢谢 – Mani5556 2015-03-31 16:59:22

+0

这是非常有用的,我必须重新编译我的服务可执行文件才能使其工作,一旦我添加了这个代码,那对我来说不是问题。 – 2016-07-08 11:20:39