2009-09-20 36 views
94

我有一個.Net Windows服務。我想創建一個安裝程序來安裝該Windows服務。用於Windows服務的Inno安裝程序?

基本上,它必須做到以下幾點:

  1. installutil.exe(需要它?)
  2. 運行installutil.exe MyService.exe
  3. 開始爲MyService

另外,我想提供運行以下命令的卸載程序:

installutil.exe /u MyService.exe 

如何使用Inno Setup來完成這些操作?

+0

我想你需要使用[Run]部分。看[這裏](http://www.vincenzo.net/isxkb/index.php?title = Inno_Setup_Help _-_'Run'_and_'UninstallRun'_sections) – 2009-09-20 01:16:20

回答

206

你不需要installutil.exe並且可能你甚至沒有權利重新發布它。

這裏是我做在我的應用程序的方式:

using System; 
using System.Collections.Generic; 
using System.Configuration.Install; 
using System.IO; 
using System.Linq; 
using System.Reflection; 
using System.ServiceProcess; 
using System.Text; 

static void Main(string[] args) 
{ 
    if (System.Environment.UserInteractive) 
    { 
     string parameter = string.Concat(args); 
     switch (parameter) 
     { 
      case "--install": 
       ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); 
       break; 
      case "--uninstall": 
       ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); 
       break; 
     } 
    } 
    else 
    { 
     ServiceBase.Run(new WindowsService()); 
    } 
} 

基本上你可以有你的服務通過如在我的例子使用ManagedInstallerClass安裝在自己/卸載。

那麼它只是物質添加到您的InnoSetup腳本是這樣的:

[Run] 
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--install" 

[UninstallRun] 
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--uninstall" 
+0

非常感謝!這工作像魔術。 再一次澄清。如何在Inno Script中運行像net start WinServ這樣的命令? – devnull 2009-09-20 02:13:57

+3

你可以試試'Filename:「net.exe」;參數:「啓動WinServ」。如果它不起作用,你可以再添加一個switch --start到你的c#應用程序,並通過使用ServiceController類直接從程序啓動windows服務(http://msdn.microsoft.com/en-us/library/ system.serviceprocess.servicecontroller.aspx)。 – 2009-09-20 02:52:42

+0

是的,太工作了! - 啓動ServiceController類。 再次感謝! – devnull 2009-09-20 03:14:13

1

如果你想避免重新啓動時的用戶升級,那麼你需要複製EXE之前停止服務之後再次啓動。

有一些腳本的功能要做到這一點,在Service - Functions to Start, Stop, Install, Remove a Service

+0

在你的鏈接文章中,所使用函數的原型沒有被精確地翻譯,它們的用法也不正確(例如,沒有等待服務啓動,停止等等。)。 – TLama 2014-12-11 05:27:20

1

您可以使用

Exec(
    ExpandConstant('{sys}\sc.exe'), 
    ExpandConstant('create "MyService" binPath= {app}\MyService.exe start= auto DisplayName= "My Service" obj= LocalSystem'), 
    '', 
    SW_HIDE, 
    ewWaitUntilTerminated, 
    ResultCode 
    ) 

創建服務。參見如何啓動「SC.EXE」,停止檢查服務狀態,刪除服務等

6

我是這樣做的:

Exec(ExpandConstant('{dotnet40}\InstallUtil.exe'), ServiceLocation, '', SW_HIDE, ewWaitUntilTerminated, ResultCode); 

顯然,創新安裝有以下常量參考你的系統上的.NET文件夾:

  • {} dotnet11
  • {} dotnet20
  • {} dotnet2032
  • {dotnet2064}
  • {dotnet40}
  • {dotnet4032}
  • {dotnet4064}可用here

的更多信息。

相關問題