C# Inno Setup for Windows 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1449994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:39:03  来源:igfitidea点击:

Inno Setup for Windows service?

c#windows-servicesinno-setup

提问by devnull

I have a .Net Windows service. I want to create an installer to install that windows service.

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

Basically, it has to do the following:

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

  1. Pack installutil.exe(Is it required?)
  2. Run installutil.exeMyService.exe
  3. Start MyService
  1. installutil.exe(需要吗?)
  2. 运行installutil.exeMyService.exe
  3. 启动我的服务

Also, I want to provide an uninstaller which runs the following command:

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

installutil.exe /u MyService.exe

How to do these using Inno Setup?

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

采纳答案by lubos hasko

You don't need installutil.exeand probably you don't even have rights to redistribute it.

您不需要installutil.exe,甚至可能没有重新分发它的权利。

Here is the way I'm doing it in my application:

这是我在应用程序中执行此操作的方式:

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());
    }
}

Basically you can have your service to install/uninstall on its own by using ManagedInstallerClassas shown in my example.

基本上,您可以使用ManagedInstallerClass我的示例中所示的方法自行安装/卸载您的服务。

Then it's just matter of adding into your InnoSetup script something like this:

然后只需将以下内容添加到 InnoSetup 脚本中即可:

[Run]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--install"

[UninstallRun]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--uninstall"

回答by Tony Edgecombe

If you want to avoid reboots when the user upgrades then you need to stop the service before copying the exe and start again after.

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

There are some script functions to do this at Service - Functions to Start, Stop, Install, Remove a Service

有一些脚本函数可以在Service - Functions to Start, Stop, Install, Remove a Service 中执行此操作

回答by Steven

You can use

您可以使用

Exec(
    ExpandConstant('{sys}\sc.exe'),
    ExpandConstant('create "MyService" binPath= {app}\MyService.exe start= auto DisplayName= "My Service" obj= LocalSystem'), 
    '', 
    SW_HIDE, 
    ewWaitUntilTerminated, 
    ResultCode
    )

to create a service. See "sc.exe" on how to start, stop, check service status, delete service, etc.

创建服务。有关如何启动、停止、检查服务状态、删除服务等信息,请参见“ sc.exe”。

回答by breez

Here's how i did it:

这是我如何做到的:

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

Apparently, Inno setup has the following constants for referencing the .NET folder on your system:

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

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

More information available here.

此处提供更多信息。