dev*_*ull 104 c# windows-services inno-setup
我有一个.Net Windows服务.我想创建一个安装程序来安装该Windows服务.
基本上,它必须执行以下操作:
installutil.exe
(需要吗?)installutil.exe
MyService.exe另外,我想提供一个运行以下命令的卸载程序:
installutil.exe /u MyService.exe
Run Code Online (Sandbox Code Playgroud)
如何使用Inno Setup进行这些操作?
lub*_*sko 228
您不需要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());
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,您可以使用自己的服务来自行安装/卸载,ManagedInstallerClass
如我的示例所示.
然后,只需在你的InnoSetup脚本中添加如下内容:
[Run]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--install"
[UninstallRun]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--uninstall"
Run Code Online (Sandbox Code Playgroud)
这是我如何做到的:
Exec(ExpandConstant('{dotnet40}\InstallUtil.exe'), ServiceLocation, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
Run Code Online (Sandbox Code Playgroud)
显然,Inno安装程序具有以下常量,用于引用系统上的.NET文件夹:
更多信息请点击此处.
您可以使用
Exec(
ExpandConstant('{sys}\sc.exe'),
ExpandConstant('create "MyService" binPath= {app}\MyService.exe start= auto DisplayName= "My Service" obj= LocalSystem'),
'',
SW_HIDE,
ewWaitUntilTerminated,
ResultCode
)
Run Code Online (Sandbox Code Playgroud)
创建服务。有关如何启动,停止,检查服务状态,删除服务等的信息,请参见“ sc.exe ”。