start windows service automatically after installation

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();
        }
     }

Answers


Need Your Help

How to keep tab's activities history while switching tabs

android android-activity tabactivity android-tabactivity

In my app I have TabActivity , and 3 Tabs in it. Every tab has some buttons, that changes current Tabs view. I mean when I'm clicking on the button, it changes the Activity in the current Tab, but ...

How to get all sub-elements of an element tree with Python ElementTree?

python xml elementtree

I want to find a way to get all the sub-elements of an element tree like the way ElementTree.getchildren() does, since getchildren() is deprecated since Python version 2.7, I don't want to use it

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.