使用ManagedInstallerClass安装C#服务

我一直试图无法安装此服务.

我目前正在使用InnoSetup,因为Visual Studio安装程序对我来说并不完全有意义,说实话(它也是凌晨1点.D 🙂

我从这个帖子中获取了一些代码:Inno Setup for Windows service?

那里的每个人都说它对他们来说很有效,但他们并没有完全解释他们做了什么或者他们把代码放在哪里.它是控制台应用程序吗?哪里?

所以,我把它放在我认为应该去的地方.将安装程序类添加到服务时,会创建一个“Program.cs”类,这就是我所说的.

这是我的’Program.cs’:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration.Install;
using System.Reflection;

namespace Installer
{
    static class Program
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {
        Console.WriteLine("MASDjhd");
        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;
          }

        }
    }
}

这是我的InnoScript:

[Setup]
AppName=MachineVerification
AppVersion=1.0
DefaultDirName={pf}\MachineVerification
DefaultGroupName=MachineVerification
UninstallDisplayIcon={app}\MachineVerification.exe
Compression=lzma2
SolidCompression=yes

[Files]
Source: "Installer.exe"; DestDir: "{app}"

[Run]
Filename:"{app}\Installer.exe"; Parameters: "--install"

[UninstallRun]
Filename: "{app}\Installer.exe"; Parameters: "--uninstall"

救命? d:

最佳答案 在这里找到我的答案:
Self install windows service in .NET c#

对于那些想要关注链接的人,解决方案是添加:

        var processInstaller = new ServiceProcessInstaller();
        var serviceInstaller = new ServiceInstaller();

        //set the privileges
        processInstaller.Account = ServiceAccount.LocalSystem;

        serviceInstaller.DisplayName = "MachineVerification";
        serviceInstaller.StartType = ServiceStartMode.Automatic;

        //must be the same as what was set in Program's constructor
        serviceInstaller.ServiceName = "MachineVerification";
        this.Installers.Add(processInstaller);
        this.Installers.Add(serviceInstaller);

到服务中的安装类的构造函数.

点赞