0

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);
}
Leandro Soares
  • 2,902
  • 2
  • 27
  • 39
Mr.Toxy
  • 357
  • 4
  • 19
  • Does the service receive parameters? – Leandro Soares Apr 05 '16 at 10:19
  • This code works with a simple windows service i've. What's the problem? – Leandro Soares Apr 05 '16 at 10:24
  • System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(ExeFilename,null); that null represents what? cause it says something about a command line. I need this to work with the path to the .exe of the service – Mr.Toxy Apr 05 '16 at 10:27
  • It gives you an error? What error? That null means that you aren't passing commands. – Leandro Soares Apr 05 '16 at 10:31
  • I have to debug the whole thing in order to check the paths and functions. Once I get to that funcion I will post the message it gives me – Mr.Toxy Apr 05 '16 at 10:40

1 Answers1

0
 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

Mr.Toxy
  • 357
  • 4
  • 19