C# window服务操作
生活随笔
收集整理的這篇文章主要介紹了
C# window服务操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public int GetWindowsServiceStartType(String sServiceName) //判斷服務狀態是手動還是禁用還是自動 {string sState = "";try{System.Diagnostics.ProcessStartInfo objProcessInf = new System.Diagnostics.ProcessStartInfo();objProcessInf.FileName = "cmd.exe";objProcessInf.UseShellExecute = false; //是否使用系統shell啟動進程 objProcessInf.RedirectStandardError = true;//錯誤信息寫入流objProcessInf.RedirectStandardInput = true;objProcessInf.RedirectStandardOutput = true; //應用程序的輸出寫入流 objProcessInf.CreateNoWindow = true; //是否在新窗口啟動該進程 objProcessInf.Arguments = "/c sc qc " + sServiceName; //啟動參數 System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(objProcessInf); //啟動進程 String sStateValue = objProcess.StandardOutput.ReadToEnd(); //讀取應用程序輸出的流if (sStateValue.IndexOf("AUTO_START") > 0){sState = "AUTO_START";return 2;}if (sStateValue.IndexOf("DEMAND_START") > 0){sState = "DEMAND_START";return 3;}if (sStateValue.IndexOf("DISABLED") > 0){sState = "DISABLED";return 4;}return 0;}catch (Exception e){sState = e.Message;return -1;}}public Boolean SetWindowsServiceStartType(String sServiceName, int iStartType) //設置服務狀態是手動,自動,還是禁用 {try{System.Diagnostics.ProcessStartInfo objProcessInf = new System.Diagnostics.ProcessStartInfo();objProcessInf.FileName = "cmd.exe";objProcessInf.CreateNoWindow = false;objProcessInf.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;string sStartState = "boot";switch (iStartType){case 1:{sStartState = "system";break;}case 2:{sStartState = "auto";break;}case 3:{sStartState = "demand";break;}case 4:{sStartState = "disabled";break;}default:{break;}}objProcessInf.Arguments = "/c sc config " + sServiceName + " start= " + sStartState;System.Diagnostics.Process.Start(objProcessInf);return true;}catch{return false;}}
?
ServiceController sc = new ServiceController("Themes");if (sc.Status.Equals(ServiceControllerStatus.Running)){sc.Stop();sc.Refresh();}if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending))){sc.Start();sc.Refresh();}
?
bool exist = false;ServiceController[] Services = ServiceController.GetServices();for (int i = 0; i < Services.Length; i++){if (Services[i].DisplayName.ToString() == textBox1.Text.Trim())exist = true;}return exist;
?
轉載于:https://www.cnblogs.com/enych/p/8946015.html
總結
以上是生活随笔為你收集整理的C# window服务操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python Web学习笔记之Pytho
- 下一篇: 一般水磨石地面价格多少一平方