예제 #1
0
        /// <summary>
        /// Installe ou désinstalle le service du gestionnaire de services
        /// </summary>
        /// <param name="displayName">Libellé dans le gestionnaire de services</param>
        /// <param name="accountUserName">Nom du compte utilisateur pour ouverture de session du service</param>
        /// <param name="accountPassword">Mot de passe du compte utilisateur pour ouverture de session du service</param>
        public static void Install(string displayName, string internalName, string accountUserName, string accountPassword)
        {
            /*
            TransactedInstaller installer = new TransactedInstaller();
            installer.Context = new InstallContext(logFilePath, new string[0]);
            // La classe AssemblyInstaller va spécifier au ServiceInstaller l'assembly correspondant
            // au service
            installer.Installers.Add(new AssemblyInstaller(Assembly.GetAssembly(serviceType)));
            // ou
            installer.Context.Parameters["assemblypath"] = Assembly.GetAssembly(serviceType).Location;
            // ou
            installer.Context.Parameters["assemblypath"] = Application.ExecutablePath + " -service";

            ServiceProcessInstaller spi = new ServiceProcessInstaller();
            installer.Installers.Add(spi);

            ServiceInstaller si = new ServiceInstaller();
            installer.Installers.Add(si);
             */

            // Détermination du type de compte d'ouverture de session
            ServiceAccount serviceAccount = ServiceAccount.LocalSystem;
            if (!string.IsNullOrEmpty(accountUserName))
            {
                if (accountUserName.ToUpper() == "LOCALSYSTEM")
                {
                    serviceAccount = ServiceAccount.LocalSystem;
                    accountUserName = null;
                }
                else if (accountUserName.ToUpper() == "LOCALSERVICE")
                {
                    serviceAccount = ServiceAccount.LocalService;
                    accountUserName = null;
                }
                else if (accountUserName.ToUpper() == "NETWORKSERVICE")
                {
                    serviceAccount = ServiceAccount.NetworkService;
                    accountUserName = null;
                }
                else
                    serviceAccount = ServiceAccount.User;
            }

            string installerLogFileName = Application.ExecutablePath + ".install.log";
            if (CommandLine.ContainsSwitch("InstallLog"))
            {
                installerLogFileName = CommandLine.GetValue("InstallLog");
                if (string.IsNullOrEmpty(installerLogFileName)) throw new Exception("Le paramètre 'InstallLog' a été précisé sur la ligne de commande mais aucun nom de fichier n'a été spécifié");
            }

            TransactedInstaller installer = new TransactedInstaller();
            installer.Context = new InstallContext(installerLogFileName, Environment.GetCommandLineArgs());
            installer.Context.Parameters["assemblypath"] = Application.ExecutablePath;

            ServiceProcessInstaller spi = new ServiceProcessInstaller();
            spi.Account = serviceAccount;
            spi.Username = accountUserName;
            spi.Password = accountPassword;
            installer.Installers.Add(spi);

            ServiceInstallerEx si = new ServiceInstallerEx();
            si.ServiceName = internalName;
            si.DisplayName = displayName;
            si.CommandLineOptions = "-service";
            installer.Installers.Add(si);

            installer.Install(new Hashtable());
        }
예제 #2
0
        /// <summary>
        /// Installe ou désinstalle le service du gestionnaire de services
        /// </summary>
        /// <param name="uninstall"></param>
        public static void Uninstall(string internalName)
        {
            string installerLogFileName = Application.ExecutablePath + ".install.log";
            if (CommandLine.ContainsSwitch("InstallLog"))
            {
                installerLogFileName = CommandLine.GetValue("InstallLog");
                if (string.IsNullOrEmpty(installerLogFileName)) throw new Exception("Le paramètre 'InstallLog' a été précisé sur la ligne de commande mais aucun nom de fichier n'a été spécifié");
            }

            TransactedInstaller installer = new TransactedInstaller();
            installer.Context = new InstallContext(installerLogFileName, Environment.GetCommandLineArgs());
            installer.Context.Parameters["assemblypath"] = Application.ExecutablePath;

            ServiceProcessInstaller spi = new ServiceProcessInstaller();
            installer.Installers.Add(spi);

            ServiceInstallerEx si = new ServiceInstallerEx();
            si.ServiceName = internalName;
            installer.Installers.Add(si);

            installer.Uninstall(null);
        }