我有一个用c#编写的Windows服务应用程序,我试图在安装后立即启动它。该服务安装正常并且没有错误或异常,但是当我打开任务管理器并在安装后检查我的服务时,状态“停止”。我可以右键单击并启动服务,它工作正常。我知道提交发生在所有安装程序之后。我已经关注了堆栈溢出的几个例子,它仍然无法启动。
以下是我的代码的一小部分,任何想法如何让它在安装后自动启动?
public example_project()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
//set the privileges
processInstaller.Account = ServiceAccount.LocalSystem;
processInstaller.Username = null;
processInstaller.Password = null;
//service properties
serviceInstaller.DisplayName = "example project";
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "example project";
this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
//update appconfig
Console.WriteLine("Please enter the directory path of the of the target folder . . .");
string target = Console.ReadLine();
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Add("target_directory", target);
config.Save(ConfigurationSaveMode.Minimal);
this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}
void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
using (ServiceController controller = new ServiceController("example project"))
{
controller.Start();
}
}