C#對WIindows服務進行操作

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   使用   ar   for   div   

  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服務進行操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.