[英]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.