C#操作服务类

来源:互联网 发布:海外游戏数据分析师 编辑:程序博客网 时间:2024/04/27 23:58

001//一、安装服务:
002private void InstallService(IDictionary stateSaver, string filepath)
003        {
004            try
005            {
006                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController("ServiceName");
007                if(!ServiceIsExisted("ServiceName"))
008                {
009                    //Install Service
010                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
011                    myAssemblyInstaller.UseNewContext = true;
012                    myAssemblyInstaller.Path =filepath;
013                    myAssemblyInstaller.Install(stateSaver);
014                    myAssemblyInstaller.Commit(stateSaver);
015                    myAssemblyInstaller.Dispose();
016                    //--Start Service
017                    service.Start();
018                }
019                else
020                {
021                    if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
022                    {
023                        service.Start();
024                    }
025                }
026            }
027            catch (Exception ex)
028            {
029                throw new Exception("installServiceError/n" + ex.Message);
030            }
031        }
032 
033//二、卸载windows服务:
034        private void UnInstallService(string filepath)
035        {
036            try
037            {
038                if (ServiceIsExisted("ServiceName"))
039                {
040                    //UnInstall Service
041                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
042                    myAssemblyInstaller.UseNewContext = true;
043                    myAssemblyInstaller.Path = filepath;
044                    myAssemblyInstaller.Uninstall(null);
045                    myAssemblyInstaller.Dispose();
046                }
047            }
048            catch (Exception ex)
049            {
050                throw new Exception("unInstallServiceError/n" + ex.Message);
051            }
052        }
053 
054//三、判断window服务是否存在:
055        private bool ServiceIsExisted(string serviceName)
056        {
057            ServiceController[] services = ServiceController.GetServices();
058            foreach (ServiceController s in services)
059            {
060                if (s.ServiceName == serviceName)
061                {
062                    return true;
063                }
064            }
065            return false;
066        }
067 
068//四、启动服务:
069private void StartService(string serviceName)
070        {
071            if (ServiceIsExisted(serviceName))
072            {
073                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
074                if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
075                {
076                    service.Start();
077                    for (int i = 0; i < 60; i++)
078                    {
079                        service.Refresh();
080                        System.Threading.Thread.Sleep(1000);
081                        if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
082                        {
083                            break;
084                        }
085                        if (i == 59)
086                        {
087                            throw new Exception(startServiceError.Replace("$s$", serviceName));
088                        }
089                    }
090                }
091            }
092        }
093 
094//五、停止服务:
095        private void StopService(string serviceName)
096        {
097            if (ServiceIsExisted(serviceName))
098            {
099                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
100                if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
101                {
102                    service.Stop();
103                    for (int i = 0; i < 60; i++)
104                    {
105                        service.Refresh();
106                        System.Threading.Thread.Sleep(1000);
107                        if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
108                        {
109                            break;
110                        }
111                        if (i == 59)
112                        {
113                            throw new Exception(stopServiceError.Replace("$s$", serviceName));
114                        }
115                    }
116                }
117            }
118        }
119  
120// 停止指定的服务
121public string StartService(string serviceName)
122{
123string strRst = null;
124ManagementObject mo = this.managementClass.CreateInstance();
125mo.Path = new ManagementPath(this.strPath+".Name=/""+serviceName+"/"");
126try
127{
128if((string)mo["State"]=="Stopped")//!(bool)mo["AcceptStop"]
129mo.InvokeMethod("StartService",null);
130}
131catch(ManagementException e)
132{
133strRst =e.Message;
134}
135return strRst;
136}
137// 暂停指定的服务
138public string PauseService(string serviceName)
139{
140string strRst = null;
141ManagementObject mo = this.managementClass.CreateInstance();
142mo.Path = new ManagementPath(this.strPath+".Name=/""+serviceName+"/"");
143try
144{
145//判断是否可以暂停
146if((bool)mo["acceptPause"]&&(string)mo["State"]=="Running")
147mo.InvokeMethod("PauseService",null);
148}
149catch(ManagementException e)
150{
151strRst =e.Message;
152}
153return strRst;
154}
155// 恢复指定的服务
156public string ResumeService(string serviceName)
157{
158string strRst = null;
159ManagementObject mo = this.managementClass.CreateInstance();
160mo.Path = new ManagementPath(this.strPath+".Name=/""+serviceName+"/"");
161try
162{
163//判断是否可以恢复
164if((bool)mo["acceptPause"]&&(string)mo["State"]=="Paused")
165mo.InvokeMethod("ResumeService",null);
166}
167catch(ManagementException e)
168{
169strRst =e.Message;
170}
171return strRst;
172}
173// 停止指定的服务
174public string StopService(string serviceName)
175{
176string strRst = null;
177ManagementObject mo = this.managementClass.CreateInstance();
178mo.Path = new ManagementPath(this.strPath+".Name=/""+serviceName+"/"");
179try
180{
181//判断是否可以停止
182if((bool)mo["AcceptStop"])//(string)mo["State"]=="Running"
183mo.InvokeMethod("StopService",null);
184}
185catch(ManagementException e)
186{
187strRst =e.Message;
188}
189return strRst;
190}
191}
192}