Inno Setup for Windows服务?

dev*_*ull 104 c# windows-services inno-setup

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

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

  1. installutil.exe(需要吗?)
  2. 运行installutil.exeMyService.exe
  3. 启动MyService

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

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)

  • 从4.5中的InstallHelper方法的文档 - "此API支持.NET Framework基础结构,不应直接在您的代码中使用." 收到System.InvalidOperationException后发现. (10认同)
  • 对于C#新手(像我一样),您需要在上面的代码中添加`using System.Reflection;`或将`Assembly`更改为`System.Reflection.Assembly`. (7认同)
  • 'ManagedInstallerClass'在'System.Configuration.Install'程序集中,添加对它的引用,如果不存在的话. (5认同)
  • 你可以试试`Filename:"net.exe"; 参数:"启动WinServ"`.如果它不起作用,你可以再添加一个开关--start到你的c#应用程序并使用ServiceController类直接从程序启动windows服务(http://msdn.microsoft.com/en-us/library/ system.serviceprocess.servicecontroller.aspx). (3认同)
  • +1很好.另见http://stackoverflow.com/questions/255056/install-a-net-windows-service-without-installutil-exe (2认同)

bre*_*eez 8

这是我如何做到的:

Exec(ExpandConstant('{dotnet40}\InstallUtil.exe'), ServiceLocation, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
Run Code Online (Sandbox Code Playgroud)

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

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

更多信息请点击此处.


Ste*_*ven 5

您可以使用

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