C#對Windows服務組的啟動與停止

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

Windows服務大家都不陌生,Windows服務組的概念,貌似MS並沒有這個說法。

作為一名軟體開發人員,我們的機器上安裝有各種開發工具,伴隨著各種相關服務。

Visual Studio可以不開啟,SqlServer Management Studio可以不開啟,但是SqlServer服務卻預設開啟了。下班後,我的電腦想用於生活、娛樂,不需要資料庫服務這些東西,尤其是在安裝了Oracle資料庫後,我感覺機器吃力的很。

每次開機後去依次關閉服務,或者設定手動開啟模式,每次工作使用時依次去開啟服務,都是一件很麻煩的事情。因此,我講這些相關服務進行打包,打包為一個服務組的概念,並通過程式來實現服務的啟動和停止。

這樣我就可以設定SqlServer、Oracle、Vmware等的服務為手動開啟,然後在需要的時候選擇開啟。

以上廢話為工具編寫背景,也是一個應用情境描述,下邊附上代碼。

服務組的定義,我使用了INI設定檔,一個配置節為一個伺服器組,配置節內的Key、Value為服務描述和服務名稱。

配置內容的先後決定了服務開啟的順序,因此類似Oracle這樣的對於服務開啟先後順序有要求的,要定義好服務組內的先後順序。

Value值為服務名稱,服務名稱並非services.msc查看的名稱欄位的值,右鍵服務,可以看到,顯示的名稱其實是服務的顯示名稱,這裡需要的是服務名稱。

設定檔如所示

註:INI檔案格式:

[Section1]

key1=value1

key2=value2

程式啟動,主表單載入,擷取配置節,即服務組。

1 string path = Directory.GetCurrentDirectory() + "/config.ini";2 List<string> serviceGroups = INIHelper.GetAllSectionNames(path);3 cboServiceGroup.DataSource = serviceGroups;

其中的INI服務類,參考連結:http://www.cnblogs.com/mahongbiao/p/3751153.html

服務的啟動和停止,需要引入System.ServiceProcess程式集。

啟動服務組:

 1 if (string.IsNullOrEmpty(cboServiceGroup.Text)) 2 { 3     MessageBox.Show("請選擇要操作的服務組"); 4     return; 5 } 6 // 7 string path = Directory.GetCurrentDirectory() + "/config.ini"; 8 string section = cboServiceGroup.Text; 9 string[] keys;10 string[] values;11 INIHelper.GetAllKeyValues(section, out keys, out values, path);12 //13 foreach (string value in values)14 {15     ServiceController sc = new ServiceController(value);16     //17     try18     {19         ServiceControllerStatus scs = sc.Status;20         if (scs != ServiceControllerStatus.Running)21         {22             try23             {24                 sc.Start();25             }26             catch (Exception ex)27             {28                 MessageBox.Show("服務啟動失敗\n" + ex.ToString());29             }30         }31     }32     catch (Exception ex)33     {34         MessageBox.Show("不存在服務" + value);35     }36     //  37 }38 //39 MessageBox.Show("服務啟動完成");

停止服務組

 1 if (string.IsNullOrEmpty(cboServiceGroup.Text)) 2 { 3     MessageBox.Show("請選擇要操作的服務組"); 4     return; 5 } 6 // 7 string path = Directory.GetCurrentDirectory() + "/config.ini"; 8 string section = cboServiceGroup.Text; 9 string[] keys;10 string[] values;11 INIHelper.GetAllKeyValues(section, out keys, out values, path);12 //13 foreach (string value in values)14 {15     ServiceController sc = new ServiceController(value);16     try17     {18         ServiceControllerStatus scs = sc.Status;19         if (scs != ServiceControllerStatus.Stopped)20         {21             try22             {23                 sc.Stop();24             }25             catch (Exception ex)26             {27                 MessageBox.Show("服務停止失敗\n" + ex.ToString());28             }29         }30     }31     catch (Exception ex)32     {33         MessageBox.Show("不存在服務" + value);34     }35     //36 37 }38 //39 MessageBox.Show("服務停止完成");40 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.