自动开始安装Windows服务 [英] Automatically start a Windows Service on install
问题描述
我有我安装使用InstallUtil.exe其中一个Windows服务。尽管我已经设置了启动方法自动,服务不启动安装的时候,我必须手动打开该服务,然后单击开始。有没有启动它或者通过命令行,或通过服务的code?
I have a Windows Service which I install using the InstallUtil.exe. Even though I have set the Startup Method to Automatic, the service does not start when installed, I have to manually open the services and click start. Is there a way to start it either via the command line, or through the code of the Service?
推荐答案
在您安装类中,添加的处理程序AfterInstall事件。然后,您可以调用ServiceController的事件处理程序来启动服务。
In your Installer class, add a handler for the AfterInstall event. You can then call the ServiceController in the event handler to start the service.
public ServiceInstaller()
{
//... Installer code here
this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}
void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
using (ServiceController sc = new ServiceController(serviceInstaller.ServiceName))
{
sc.Start();
}
}
现在,当您在安装运行InstallUtil它将安装然后启动该服务。
Now when you run InstallUtil on your installer it will install and then start up the service.
这篇关于自动开始安装Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!