2011年7月16日星期六

在应用程序中安装、卸载、启动和停止系统服务

using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.ServiceProcess;

namespace TncsConfig
{
    class ServiceManager
    {
        private String serviceName;
        private String filepath;

        public ServiceManager(string serviceName,string filepath){
            this.serviceName = serviceName;
            this.filepath = filepath;
        }
       
        public void InstallService(IDictionary stateSaver)
        {
            try
            {
                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                if (!ServiceIsExisted())
                {
                    //Install Service
                    string[] commandLineOptions = new string[1] { "/LogFile=TncsDataTransferService.log" };
                    System.Configuration.Install.AssemblyInstaller myAssemblyInstaller = new System.Configuration.Install.AssemblyInstaller(filepath,commandLineOptions);
                    myAssemblyInstaller.UseNewContext = true;
                    myAssemblyInstaller.Install(stateSaver);
                    myAssemblyInstaller.Commit(stateSaver);
                    myAssemblyInstaller.Dispose();
                    //--Start Service
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running);
                }
                else
                {
                    if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                    {
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Install Service Error: " + ex.Message);
            }
        }

        public void UnInstallService()
        {
            try
            {
                if (ServiceIsExisted())
                {
                    //UnInstall Service
                    System.Configuration.Install.AssemblyInstaller myAssemblyInstaller = new System.Configuration.Install.AssemblyInstaller();
                    myAssemblyInstaller.UseNewContext = true;
                    myAssemblyInstaller.Path = filepath;
                    myAssemblyInstaller.Uninstall(null);
                    myAssemblyInstaller.Dispose();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Uninstall Service Error: " + ex.Message);
            }
        }

        public bool ServiceIsExisted()
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController s in services)
            {
                if (s.ServiceName == serviceName)
                {
                    return true;
                }
            }
            return false;
        }

        public Boolean ServiceIsRunning(){
            if (ServiceIsExisted())
            {
                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                    return true;
                else
                    return false;
            }
            return false;
        }

        public void StartService()
        {
            if (ServiceIsExisted())
            {
                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                {
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running);
                }
            }
        }

        public void StopService()
        {
            if (ServiceIsExisted())
            {
                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped);
                }
            }
        }
    }
}

没有评论:

发表评论