首页 > 代码库 > C#对WIindows服务进行操作

C#对WIindows服务进行操作

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Threading.Tasks;  6 using System.Collections.Generic;  7 using System.Windows;  8 using Microsoft.Win32;  9 using System.Security; 10 using System.Diagnostics; 11 using System.ServiceProcess; 12 using System.Threading; 13 using System.Configuration.Install; 14  15  16  17  18 namespace UpdateModule 19 { 20     public class DetectService 21     { 22         /* 23          * 首先需要添加System.ServiceProcess.dll引用 24          *  25          */ 26         //如果Windows服务处于停止状态,启动Windows服务 27  28         //static void Main(string[] args) 29         //{ 30         //    //StopWindowsService("SMService"); 31         //    StartWindowsService("SMService"); 32         //    //UnInstallService(); 33         //    //Process.Start("net stop SMService"); 34         //    //string str = Console.ReadLine(); 35  36  37         //} 38         //开启Windows服务 39         public static int StartWindowsService(string serviceName) 40         { 41             ServiceController[] scs = ServiceController.GetServices(); 42             //0表示未启动状态 43             int bResult = 0; 44             foreach (ServiceController sc in scs) 45             { 46  47                 if (sc.DisplayName.Contains(serviceName) && sc.Status == ServiceControllerStatus.Stopped) 48                 { 49                         try 50                         { 51                             sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30)); 52                             sc.Start(); 53                             //让线程睡眠1秒钟,等待其他服务的加载,不设置则会出错 54                             Thread.Sleep(1000); 55                             //1表示启动状态 56                             bResult = 1; 57                         } 58                         catch (Exception ex) 59                         { 60                             //2表示启动失败 61                             bResult = 2; 62                             throw ex; 63                         } 64                 }  //C#启动Windows服务及关闭     65             } 66             return bResult; 67         } 68  69         //停止Windows服务 70         public static bool StopWindowsService(string serviceName) 71         { 72             ServiceController[] scs = ServiceController.GetServices(); 73             bool bResult = false; 74             foreach (ServiceController sc in scs) 75             { 76                 if (sc.DisplayName.Contains(serviceName)) 77                 { 78                     try 79                     { 80                         sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30)); 81                         sc.Stop(); 82                         bResult = true; 83                     } 84                     catch (Exception ex) 85                     { 86                         bResult = false; 87                         throw ex; 88                     } 89                 } 90             } 91             return bResult; 92         } 93  94         //卸载Windows服务 95         public static void UnInstallService() 96         { 97  98             System.Diagnostics.Process p = new System.Diagnostics.Process(); 99             p.StartInfo.FileName = "cmd.exe";100             p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动101             p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息102             p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息103             p.StartInfo.RedirectStandardError = true;//重定向标准错误输出104             p.StartInfo.CreateNoWindow = true;//不显示程序窗口105             p.Start();//启动程序106 107             //向cmd窗口发送输入信息108             p.StandardInput.WriteLine(@"sc delete  SMService" + "&exit");109             //p.StandardInput.AutoFlush = true;110             ////获取cmd窗口的输出信息111             //string output = p.StandardOutput.ReadToEnd();112             p.WaitForExit();//等待程序执行完退出进程113             p.Close();114 115 116             //Console.WriteLine(output);117             //Console.ReadKey();118 119         }120 121     }122 }

 

C#对WIindows服务进行操作