106

我有一个 .Net Windows 服务。我想创建一个安装程序来安装该 Windows 服务。

基本上,它必须执行以下操作:

  1. installutil.exe(需要吗?)
  2. 运行installutil.exe我的服务.exe
  3. 启动我的服务

另外,我想提供一个运行以下命令的卸载程序:

installutil.exe /u MyService.exe

如何使用 Inno Setup 进行这些操作?

4

5 回答 5

240

你不需要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"
于 2009-09-20T01:39:52.373 回答
8

我是这样做的:

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

显然,Inno setup 有以下常量用于引用系统上的 .NET 文件夹:

  • {dotnet11}
  • {dotnet20}
  • {dotnet2032}
  • {dotnet2064}
  • {dotnet40}
  • {dotnet4032}
  • {dotnet4064}

更多信息可在此处获得。

于 2014-10-16T09:53:56.240 回答
6

您可以使用

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 ”。

于 2010-08-15T15:33:07.720 回答
2

如果您想在用户升级时避免重新启动,那么您需要在复制 exe 之前停止服务并在之后重新启动。

服务中有一些脚本函数可以执行此操作- 启动、停止、安装、删除服务的函数

于 2009-12-23T11:35:37.297 回答
0

看看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

于 2021-03-25T10:14:36.600 回答