How to automatically start your service after install?

前端 未结 7 1220
小蘑菇
小蘑菇 2020-12-05 01:50

How do you automatically start a service after running an install from a Visual Studio Setup Project?

I just figured this one out and thought I would share the answe

7条回答
  •  伪装坚强ぢ
    2020-12-05 02:16

    This approach uses the Installer class and the least amount of code.

    using System.ComponentModel;
    using System.Configuration.Install;
    using System.ServiceProcess;
    
    namespace MyProject
    {
        [RunInstaller(true)]
        public partial class ProjectInstaller : Installer
        {
            public ProjectInstaller()
            {
                InitializeComponent();
                serviceInstaller1.AfterInstall += (sender, args) => new ServiceController(serviceInstaller1.ServiceName).Start();
            }
        }
    }
    

    Define serviceInstaller1 (type ServiceInstaller) in the Installer class designer and also set its ServiceName property in the designer.

提交回复
热议问题