如何使我的 Windows 服务以下列方式工作...
1.) 安装后自动启动
2.) 即使我们简单地双击可执行文件也会自动启动
换句话说,我不想使用“NET START”、“SC”命令,也不想通过服务控制台启动它。我只希望我的服务能够自动安装并自动启动......加上双击可执行文件时自动启动。
谢谢。
最佳答案
您可以在 commited
中使用它像这样的事件:
[RunInstaller(true)]
public class ServiceInstaller : Installer
{
string serviceName = "MyServiceName";
public ServiceInstaller()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
processInstaller.Account = ...;
processInstaller.Username = ...;
processInstaller.Password = ...;
serviceInstaller.DisplayName = serviceName;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = serviceName;
this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
this.Committed += new InstallEventHandler(ServiceInstaller_Committed);
}
void ServiceInstaller_Committed(object sender, InstallEventArgs e)
{
// Auto Start the Service Once Installation is Finished.
var controller = new ServiceController(serviceName);
controller.Start();
}
}
关于c# - 双击启动 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3171987/