安装一个服务并设置为自动启动

本文关键字:设置 自动启动 服务 一个 安装 | 更新日期: 2023-09-27 18:12:27

我目前有一个程序可以作为服务正确安装,但启动类型设置为"手动"。我如何使这个应用程序设置启动类型=自动?

static void Main(string[] args) {
        if (System.Environment.UserInteractive) {
            if (args.Length > 0) {
                switch (args[0]) {
                    case "/install": {
                        ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                        break;
                    }
                    case "/uninstall": {
                        ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                     break;
                 }
             }
         }
     } else {
         if (!Environment.UserInteractive) {
             // running as service
             using (var service = new DocLogicJMS())
                 ServiceBase.Run(service);
         } else {
             // running as console app
             Start(args);
             Console.WriteLine("Press any key to stop...");
             Console.ReadKey(true);
             Stop();
         }
     }
}

而JMS是:

namespace JMS {
partial class DocLogicJMS {
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing) {
        if (disposing && (components != null)) {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    #region Component Designer generated code
    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent() {
        this.ServiceName = "DocLogic JMS";
    }
    #endregion
}

}

安装一个服务并设置为自动启动

这个问题似乎已经在这里得到了回答:如何在。net中更改Windows服务的启动类型(安装后)?

这似乎是最受欢迎的答案:

var svc = new ServiceController("ServiceNameGoesHere");  
ServiceHelper.ChangeStartMode(svc, ServiceStartMode.Automatic);