.Net实现Windows服务安装完成后自动启动介绍

时间:2022-03-26 00:10:14

重写ProjectInstaller的Commit方法

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;


namespace CleanExpiredSessionSeivice
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            //Auot start service after the installation is completed
            ServiceController sc = new ServiceController("SeiviceName");
            if (sc.Status.Equals(ServiceControllerStatus.Stopped))
            {
                sc.Start();
            }
        }
    }
}