2009-11-09 11 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

相關問題