简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM