[英]Inno Setup for Windows service?
我有一個 .Net Windows 服務。 我想創建一個安裝程序來安裝該 Windows 服務。
基本上,它必須執行以下操作:
installutil.exe
(是否需要?)installutil.exe
MyService.exe另外,我想提供一個運行以下命令的卸載程序:
installutil.exe /u MyService.exe
如何使用 Inno Setup 執行這些操作?
您不需要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"
這是我如何做到的:
Exec(ExpandConstant('{dotnet40}\InstallUtil.exe'), ServiceLocation, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
顯然,Inno setup 有以下常量用於引用系統上的 .NET 文件夾:
此處提供更多信息。
您可以使用
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 ”。
如果您想避免在用戶升級時重新啟動,那么您需要在復制 exe 之前停止服務並在之后重新啟動。
有一些腳本函數可以在Service - Functions to Start, Stop, Install, Remove a Service 中執行此操作
看看topshelf http://topshelf-project.com/
它允許您將服務開發為控制台應用程序
將啟動/停止服務作為 API 添加到您的服務中...
...您可以從 InnoSetup 調用
[Run] Filename: "{app}\\myservice.exe"; Parameters: "stop" ; Flags : waituntilterminated Filename: "{app}\\myservice.exe"; Parameters: "uninstall" ; Flags : waituntilterminated Filename: "{app}\\myservice.exe"; Parameters: "install -description ""myservice""" ; Flags : waituntilterminated
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.