Search code examples
c#serviceinstallutil

installing a windows service with .exe path in c#


I need to install a service programatically (It even can be done with InstallUtil but it can't be done manually)

I have the following code but can't figure out how to do it properly since that code asks for some class info wich I dont know and I need to know how to use it in order to execute it using teh .exe. The code is as follows:

public static void InstallService(string ExeFilename)
{
    System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(ExeFilename,null);
    Installer.UseNewContext = true;
    Installer.Install(null);
    Installer.Commit(null);
}

Solution

  •  public void InstallService(string ExeFilename)
            {
                try
                {
                    System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(ExeFilename, null);
                    Installer.UseNewContext = true;
                    Installer.Install(null);
                    Installer.Commit(null);
                    DialogResult NovoDialog = new DialogResult();
                    NovoDialog = MessageBox.Show("Deseja Iniciar o Serviço?", "Orca ++ Updater", MessageBoxButtons.YesNo);
                    if (NovoDialog == DialogResult.Yes)
                    {
                        ServiceController service = new ServiceController("OrcaService");
                        TimeSpan timeout = TimeSpan.FromMilliseconds(1500);
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running, timeout);
    
                    }
                }
                catch (Exception ex)
                {
                    Erro NovoErro = new Erro();
                    Program.Erro = ex.ToString();
                    NovoErro.ShowDialog();
                }
            }
    

    This is how I solveed it