如何實現(xiàn)C#啟動Windows服務(wù)及關(guān)閉,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
為大東等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及大東網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站建設(shè)、成都網(wǎng)站制作、大東網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
C#啟動Windows服務(wù)及關(guān)閉實例實現(xiàn)的步驟和使用調(diào)用的類是什么呢?讓我們來看看具體的實現(xiàn):
C# 控制Windows服務(wù)的啟動與關(guān)閉
// ...... using System.ServiceProcess; // ...... /// <summary> /// Restart windows service /// </summary> /// <param name="serviceName"> ///the windows service display name</param> /// <returns> If the restart successfully ///return true else return false</returns> public static bool RestartWindowsService(string serviceName) { bool bResult = false; try { try { //C#啟動Windows服務(wù)及關(guān)閉 StopWindowsService(serviceName); Thread.Sleep(1000); } catch (Exception ex) { StartWindowsService(serviceName); Thread.Sleep(1000); StopWindowsService(serviceName); Thread.Sleep(1000); Console.WriteLine(ex.Message); } try { StartWindowsService(serviceName); Thread.Sleep(1000); } catch (Exception ex) //C#啟動Windows服務(wù)及關(guān)閉{ StopWindowsService(serviceName); Thread.Sleep(1000); StartWindowsService(serviceName); Thread.Sleep(1000); Console.WriteLine(ex.Message); } bResult = true; } catch (Exception ex) { bResult = false; throw ex; } return bResult; } //C#啟動Windows服務(wù)及關(guān)閉/// <summary> /// Start windows service /// </summary> /// <param name="serviceName"> ///the windows service display name</param> /// <returns>If the start successfully ///return true else return false</returns> public static bool StopWindowsService(string serviceName) { ServiceController[] scs = ServiceController.GetServices(); bool bResult = false; foreach (ServiceController sc in scs) { if (sc.DisplayName == serviceName) { try { sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30)); sc.Stop(); bResult = true; } catch (Exception ex) { bResult = false; throw ex; } } } return bResult; } //C#啟動Windows服務(wù)及關(guān)閉/// <summary> /// Stop windows service /// </summary> /// <param name="serviceName">the windows ///service display name</param> /// <returns>If the stop successfully ///return true else return false</returns> public static bool StartWindowsService( string serviceName) { ServiceController[] scs = ServiceController.GetServices(); bool bResult = false; foreach (ServiceController sc in scs) { if (sc.DisplayName == serviceName) { try { sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30)); sc.Start(); bResult = true; } catch (Exception ex) { bResult = false; throw ex; } } //C#啟動Windows服務(wù)及關(guān)閉 } return bResult; }
C#啟動Windows服務(wù)及關(guān)閉具體實現(xiàn)就向你介紹到這里,希望對你了解和學(xué)習(xí)C#啟動Windows服務(wù)及關(guān)閉有所幫助。
看完上述內(nèi)容,你們掌握如何實現(xiàn)C#啟動Windows服務(wù)及關(guān)閉的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當前標題:如何實現(xiàn)C#啟動Windows服務(wù)及關(guān)閉
標題鏈接:http://jinyejixie.com/article38/gpeisp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、標簽優(yōu)化、網(wǎng)站收錄、網(wǎng)站營銷、網(wǎng)站建設(shè)、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)