标签:return service 验证 c# Windows ServiceControllerStatus isbn ServiceController star
0判断服务状态
using System.ServiceProcess; // ServiceController sc; try { sc = new ServiceController(SERVICE_NAME); } catch(ArgumentException) { return "Invalid service name."; // Note that just because a name is valid does not mean the service exists. } using(sc) { ServiceControllerStatus status; try { sc.Refresh(); // calling sc.Refresh() is unnecessary on the first use of `Status` but if you keep the ServiceController in-memory then be sure to call this if you're using it periodically. status = sc.Status; } catch(Win32Exception ex) { // A Win32Exception will be raised if the service-name does not exist or the running process has insufficient permissions to query service status. // See Win32 QueryServiceStatus()'s documentation. return "Error: " + ex.Message; } switch(status) { case ServiceControllerStatus.Running: return "Running"; case ServiceControllerStatus.Stopped: return "Stopped"; case ServiceControllerStatus.Paused: return "Paused"; case ServiceControllerStatus.StopPending: return "Stopping"; case ServiceControllerStatus.StartPending: return "Starting"; default: return "Status Changing"; } }
1. 操作WINDOW服务需要在C#里引入一个类库:System.ServiceProcess
//监测服务是否启动 private bool ServicesExists(string serviceName) { bool isbn = false; //获取所有服务 ServiceController[] services = ServiceController.GetServices(); try { foreach (ServiceController service in services) { if (service.ServiceName.ToUpper() == serviceName.ToUpper()) { isbn = true; break; } } return isbn; } catch (Exception ex) { return false; } } //启动服务 private bool ServiceStar(string serviceName) { bool isbn = false; try { if (ServicesExists(serviceName)) { ServiceController star_service = new ServiceController(serviceName); if (star_service.Status != ServiceControllerStatus.Running && star_service.Status != ServiceControllerStatus.StartPending) { star_service.Start(); for (int i = 0; i < 60; i++) { star_service.Refresh(); System.Threading.Thread.Sleep(1000); if (star_service.Status == ServiceControllerStatus.Running) { isbn = true; break; } if (i == 59) { isbn = false; } } } } } catch (Exception ex) { return false; } return isbn; } //停止服务 private bool ServiceStop(string serviceName) { bool isbn = false; try { if (ServicesExists(serviceName)) { ServiceController star_service = new ServiceController(serviceName); if (star_service.Status == ServiceControllerStatus.Running) { star_service.Stop(); for (int i = 0; i < 60; i++) { star_service.Refresh(); System.Threading.Thread.Sleep(1000); if (star_service.Status == ServiceControllerStatus.Stopped) { isbn = true; break; } if (i == 59) { isbn = false; } } } } } catch (Exception ex) { return false; } return isbn; }
// 判断服务状态
try { using(ServiceController sc = new ServiceController(SERVICE_NAME)) { return sc.Status == ServiceControllerStatus.Running; } } catch(ArgumentException) { return false; } catch(Win32Exception) { return false; }
标签:return,service,验证,c#,Windows,ServiceControllerStatus,isbn,ServiceController,star 来源: https://www.cnblogs.com/lrzy/p/16638131.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。