how to make my installer / application start as a service in c #?

0

Well the thing is that I have an installer in c # but what I need is startup as a service. Well my installer is very simple such that:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Instalador
{
    [System.ComponentModel.RunInstaller(true)]
    public class Simple : System.Configuration.Install.Installer
    {

        Form4 form = new form4();
        public Simple()
        {

        }
    }
}

I read several articles in the stackoverflow community and mentioned that it was possible. I mean this thread:

link

Then my question is possible that in my installer can start it as a service either using OnBeforeInstall or whatever is necessary. Can someone advise me how to put my installer as a service or what steps should I take to put it as a service?

Could it be something like that, as they did but adapted to the form that I should use?

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Instalador
{
    [System.ComponentModel.RunInstaller(true)]
    public class Simple : System.Configuration.Install.Installer
    {
    private readonly ServiceProcessInstaller processInstaller;
    private readonly System.ServiceProcess.ServiceInstaller serviceInstaller;

    Form4 form = new form4();
    public Simple()
    {

    }

    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();
    }
}
}
    
asked by Sergio Ramos 15.02.2018 в 01:30
source

0 answers