Windows service manager

Description

installer un service windows
desinstaller un service windows
demarrer un service windows
arreter un service windows

install windows services

installutil.exe

Source / Exemple :


namespace ServiceManager
{
    using System.ServiceProcess;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using System;

    public class ManageServices
    {
        public ManageServices()
        {

        }

        public bool InstallService(string servicePath, string serviceName)
        {
            //on cree les .bat pour installer et desinstaller le service
            CreateBatFile(servicePath);

            //si le service existe deja il faut d abord l arreter
            bool serviceExists = false;
            StopService(serviceName, out serviceExists);

            //si le service existe on le desinstall
            if (serviceExists)
            {
                InstallServiceWithBat(Directory.GetCurrentDirectory(), @"uninstall.bat");
                System.Threading.Thread.Sleep(1500);
            }
            
            //on installe le service
            InstallServiceWithBat(Directory.GetCurrentDirectory(), @"install.bat");
            System.Threading.Thread.Sleep(1500);

            //on demarre le service
            StartService(serviceName);
            File.Delete(Directory.GetCurrentDirectory() + @"\install.bat");
            File.Delete(Directory.GetCurrentDirectory() + @"\uninstall.bat");
            return true;
        }

        public bool StopService(string serviceName, out bool serviceExists)
        {
            ServiceController[] scServices;
            scServices = ServiceController.GetServices();
            serviceExists = false;

            foreach (ServiceController scTemp in scServices)
            {
                if (scTemp.ServiceName.Equals(serviceName, StringComparison.CurrentCultureIgnoreCase))
                {
                    serviceExists = true;
                    try
                    {
                        if (scTemp.Status != ServiceControllerStatus.Stopped)
                        {
                            scTemp.Stop();
                            scTemp.WaitForStatus(ServiceControllerStatus.Stopped);
                        }

                        return true;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("error while stopping the service " + serviceName);
                    }
                }
            }
            return false;
        }

        public bool StartService(string serviceName)
        {

            ServiceController[] scServices;
            scServices = ServiceController.GetServices();

            foreach (ServiceController scTemp in scServices)
            {
                if (scTemp.ServiceName.Equals(serviceName, StringComparison.CurrentCultureIgnoreCase))
                {
                    try
                    {
                        if (scTemp.Status != ServiceControllerStatus.Running)
                            scTemp.Start();
                        scTemp.WaitForStatus(ServiceControllerStatus.Running);

                        return true;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("error while startting the service " + serviceName);
                    }
                }
            }

            return true;
        }

        private bool CreateBatFile(string servicePath)
        {
            //script d installation du service
            StreamWriter sw = new StreamWriter(Directory.GetCurrentDirectory() + @"\install.bat");
            string installutilPath = @"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\";
            sw.Write(string.Format("{0}installutil.exe -i {1}", installutilPath, servicePath));
            sw.Close();

            //script de desinstallation du service
            sw = new StreamWriter(Directory.GetCurrentDirectory() + @"\uninstall.bat");
            sw.Write(string.Format("{0}installutil.exe -u {1}", installutilPath, servicePath));
            sw.Close();

            return true;
        }

        private bool InstallServiceWithBat(string targetDir, string bat)
        {

            Process p = null;
            try
            {
                targetDir = string.Format(targetDir);
                p = new Process();
                p.StartInfo.WorkingDirectory = targetDir;
                p.StartInfo.FileName = bat;

                p.StartInfo.Arguments = string.Format("");
                p.StartInfo.CreateNoWindow = false;
                p.Start();
                p.WaitForExit();

                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Occurred :{0},{1}",
                            ex.Message, ex.StackTrace.ToString());
                return false;
            }
        }
    }
}

Conclusion :


util si l on developpe une solution avec plusieurs services
ce petit utiltaire permet de mettre a jour les services selectionnees

donc pas besoin de lancer tous les services depuis visual studio
on gagne pas mal de temps

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.