我有使用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*
使用ServiceController从代码开始为您服务。
更新:从命令行启动服务的更正确方法是使用“sc”(Service Controller)命令而不是“net”。
可以使用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。
在这种情况下,您也不需要私有成员。
感谢您的帮助。
- 1. 安装时自动启动Windows服务
- 2. 自动启动服务时,windows启动
- 3. 安装Windows服务将无法启动
- 4. 如何通过SC安装后自动启动Windows服务?
- 5. 如何将程序安装为在启动时自动启动的服务?
- 6. MSI安装程序启动服务启动时的自动修复
- 7. Windows服务启动时启动
- 8. 如何使Windows服务启动“自动(延时启动)”
- 9. 当系统重新启动时自动启动windows服务
- 10. 作为Windows服务安装时启动远程调试Tomcat(jdwp)
- 11. Windows服务启动时间
- 12. Windows服务启动超时
- 13. 启动时启动服务和应用程序安装后
- 14. Python Windows服务自动启动太早
- 15. Windows服务不会自动启动
- 16. 我如何启动Windows服务自动
- 17. 在没有安装项目的情况下安装时启动Windows服务
- 18. 只要Windows启动时自动运行Dropbox作为Windows服务
- 19. 启动时自动安装.vhdx
- 20. 安装sqlxml windows azure启动任务
- 21. 在Windows启动时自动启动Windows服务,这取决于Oracle
- 22. 如何检查是否安装了服务并启动服务(如果安装了其他服务,如果安装并启动Windows的厨师的服务)
- 23. 启动Windows服务
- 24. 如何在安装后自动启动您的服务?
- 25. Linuxbrew,安装的Postgres并自动启动服务
- 26. msi安装程序 - 服务未启动
- 27. 无法安装/启动mysql服务器
- 28. Windows AppFabric自动启动不启动WCF服务
- 29. 通过代码安装并启动Windows服务
- 30. 安装Windows服务后重新启动计算机
我不想这样做。我期望从命令行或Windows服务类中执行此操作。 – mickyjtwin 2009-06-24 06:41:41
对不起,我的错,我错过了你明确排除通过控制面板启动它的可能性。 – 2009-06-24 06:54:34