安装时自动启动Windows服务
我有使用InstallUtil.exe安装的Windows服务。即使我已将“启动方法”设置为“自动”,但安装后服务不会启动,但我必须手动打开服务并单击“开始”。有没有办法通过命令行或通过服务的代码启动它?安装时自动启动Windows服务
在你安装程序类中,添加的处理程序AfterInstall事件。然后您可以调用事件处理程序中的ServiceController来启动该服务。
using System.ServiceProcess;
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时,它将安装并启动该服务。
自动启动意味着服务在Windows启动时自动启动。正如其他人所说,要从控制台启动它,您应该使用ServiceController。
下面的命令怎么样?
net start "<service name>"
net stop "<service name>"
您可以使用下面的命令行来启动服务:
net start *servicename*
可以使用GetServices
方法ServiceController 类来获取所有服务的数组。然后,通过检查每个服务的ServiceName
属性来找到您的服务。当您找到您的服务时,请致电Start
方法启动它。
您还应该检查Status
属性以查看在调用开始之前它已处于什么状态(它可能正在运行,已暂停,已停止等)。
控制服务编程选项:
- 本机代码可以使用,"Starting a Service"。最大的控制与最小的依赖性,但最多的工作。
- WMI:Win32_Service有一个
StartService
方法。这对于需要执行其他处理(例如选择哪种服务)的情况非常有用。 - PowerShell:通过
RunspaceInvoke
执行Start-Service
或创建自己的Runspace
并使用其CreatePipeline
方法执行。这对于需要能够使用比WMI更容易的编码模型执行其他处理(例如选择哪种服务)的情况是很好的,但取决于安装的PSH。 - .NET应用程序可以使用
ServiceController
重构一点点之后,这是带自动启动一个完整的Windows安装程序服务的一个例子:
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace Example.of.name.space
{
[RunInstaller(true)]
public partial class ServiceInstaller : Installer
{
private readonly ServiceProcessInstaller processInstaller;
private readonly System.ServiceProcess.ServiceInstaller serviceInstaller;
public ServiceInstaller()
{
InitializeComponent();
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new System.ServiceProcess.ServiceInstaller();
// Service will run under system account
processInstaller.Account = ServiceAccount.LocalSystem;
// Service will have Start Type of Manual
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "Windows Automatic Start Service";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
serviceInstaller.AfterInstall += ServiceInstaller_AfterInstall;
}
private void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController("Windows Automatic Start Service");
sc.Start();
}
}
}
你破坏你的设计师。重新添加您的安装程序组件。它应该有一个serviceInstaller和一个serviceProcessInstaller。将属性启动方法设置为自动的serviceInstaller将在安装时以及每次重新启动后启动。
尽管完全按照接受的答案,我仍然无法启动服务 - 我在安装过程中收到了失败消息,指出刚安装的服务无法启动,因为它不存在尽管使用this.serviceInstaller.ServiceName
而不是字面...
我终于找到一个替代的解决方案,使得使用命令行:
private void serviceInstaller_AfterInstall(object sender, InstallEventArgs e) {
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C sc start " + this.serviceInstaller.ServiceName;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
}
刚一说明:您可能以不同方式利用建立的为您服务窗体界面添加服务安装程序和项目安装程序。在这种情况下,请将serviceInstaller.ServiceName替换为“name from designer”.ServiceName。
在这种情况下,您也不需要私有成员。
感谢您的帮助。
我不想这样做。我期望从命令行或Windows服务类中执行此操作。 – mickyjtwin 2009-06-24 06:41:41
对不起,我的错,我错过了你明确排除通过控制面板启动它的可能性。 – 2009-06-24 06:54:34