Inno Setup for Windows服务?

时间:2009-09-20 01:10:35

标签: c# windows-services inno-setup

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

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

  1. 打包installutil.exe(是否需要?)
  2. 运行installutil.exe MyService.exe
  3. 启动MyService
  4. 另外,我想提供一个运行以下命令的卸载程序:

    installutil.exe /u MyService.exe
    

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

5 个答案:

答案 0 :(得分:221)

您不需要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"

答案 1 :(得分:7)

以下是我的表现:

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

显然,Inno安装程序具有以下常量,用于引用系统上的.NET文件夹:

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

提供更多信息here

答案 2 :(得分:3)

您可以使用

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

答案 3 :(得分:2)

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

有一些脚本函数可以在Service - Functions to Start, Stop, Install, Remove a Service

执行此操作

答案 4 :(得分: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

相关问题