How to automatically start your service after install?

前端 未结 7 1219
小蘑菇
小蘑菇 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:18

    Add the following class to your project.

    using System.ServiceProcess;  
    
    class ServInstaller : ServiceInstaller
    {
        protected override void OnCommitted(System.Collections.IDictionary savedState)
        {
            ServiceController sc = new ServiceController("YourServiceNameGoesHere");
            sc.Start();
        }
    }
    

    The Setup Project will pick up the class and run your service after the installer finishes.

提交回复
热议问题