In our daily work, we often encounter the situation of detecting or setting system services. Generally, we mainly encounter the following Operation scenarios:
1. Obtain all system services
2. check whether a service status is enabled.
3. Set a service status: start, stop, pause, and restore.
4. obtain the status of a service
Therefore, it is more convenient to encapsulate a class and you can call it as you like without having to write the same code every time. Now, you can implement the class of the Operating System Service as follows:
System Service: systServiceMng
In fact, its core is to map to System service instances through ServiceController by referencing System. ServiceProcess. dll of the corresponding version in the System (which must be consistent with the version of the referenced project framework ).
Provides some common methods and attributes to set and obtain the service status.
Using System; using System. collections. generic; using System. linq; using System. text; using System. collections; using System. serviceProcess; // made by george // date: 2014-05-30 // QQ: 709617880 namespace systemServiceMng {// set the operation Enumeration type public enum opertType {start, stop, pause, reStart }; public class initialize servicemng {// <summary> // obtain all system service objects /// </summary> /// <returns> </returns> public ArrayList getAllsystemServices () {ArrayList arryServices = new ArrayList (); ServiceController [] services = ServiceController. getServices (); foreach (ServiceController a in services) {arryServices. add (a) ;}return arryServices ;}/// <summary> /// get all service names, return list /// </summary> /// <returns> </returns> public List <String> getAllServiceNames () {List <String> listNames = new List <String> (); ServiceController [] services = ServiceController. getServices (); foreach (ServiceController a in services) {listNames. add (. displayName);} return listNames ;} /// <summary> /// set the specified service /// </summary> /// <param name = "serviceName"> service display name </param> /// <param name = "success"> operation type </param> /// <returns> true: the setting is successful, false setting failed </returns> public Boolean setService (string serviceName, opertType failed) {Boolean isok = false; ServiceController [] allServices = System. serviceProcess. serviceController. getServices (); foreach (ServiceController SC in allServices) {if (SC. displayName. trim () = serviceName. trim () {if (partial. toString () = "start") {SC. start (); if (SC. status. toString () = "Running") isok = true;} if (response. toString () = "stop") {SC. stop (); if (SC. status. toString () = "Stopped") isok = true;} if (disabled. toString () = "pause") {SC. pause (); if (SC. status. toString () = "Pause") isok = true;} if (signature. toString () = "reStart") {SC. continue (); if (SC. status. toString () = "Running") isok = true ;}} return isok ;} /// <summary> /// check the service status /// </summary> /// <param name = "serviceName"> </param> /// <returns> </returns> public Boolean checkServiceIsRunning (string serviceName) {ServiceController [] allServices = System. serviceProcess. serviceController. getServices (); Boolean runing = false; foreach (ServiceController SC in allServices) {if (SC. displayName. trim () = serviceName. trim () {if (SC. status. toString () = "Running") {runing = true ;}} return runing ;} /// <summary> /// obtain the service status /// </summary> /// <param name = "serviceName"> service display name </param> /// <returns> </returns> public String getStatue (string serviceName) {ServiceController [] allServices = System. serviceProcess. serviceController. getServices (); foreach (ServiceController SC in allServices) {if (SC. displayName. trim () = serviceName. trim () {return SC. status. toString () ;}} return "";}}}
Note: Each Service has a CanXXX attribute such as CanStop, CanShouDown ......, Before setting the service to start, stop, pause, and recover, you must determine whether this attribute can be set. Otherwise, an error is reported.
You can write a simple winForm for operating system services. It is also a small tool that allows you to directly set system services.
[Coming soon]: C # computer series-operations on IIS services