自动开始安装Windows服务 [英] Automatically start a Windows Service on install

查看:101
本文介绍了自动开始安装Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我安装使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆