I have a windows service app written in c# and am trying to start it right after installation. The service installs fine and gives no errors or exceptions, but when I open task manager and check my services after installation the status is "stopped". I can right click and start the service and it works fine. I know the the commit occurs after all the installers. I've followed several examples on stack overflow and it still will not start.
Here is a small glimpse of my code, any ideas how to get it to start automatically after installing?
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();
}
}