C#对Windows服务组的启动与停止
作者:网络转载 发布时间:[ 2014/6/17 13:53:03 ] 推荐标签:C# net
程序启动,主窗体加载,获取配置节,即服务组。
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 try
18 {
19 ServiceControllerStatus scs = sc.Status;
20 if (scs != ServiceControllerStatus.Running)
21 {
22 try
23 {
24 sc.Start();
25 }
26 catch (Exception ex)
27 {
28 MessageBox.Show("服务启动失败
" + ex.ToString());
29 }
30 }
31 }
32 catch (Exception ex)
33 {
34 MessageBox.Show("不存在服务" + value);
35 }
36 //
37 }
38 //
39 MessageBox.Show("服务启动完成");
|
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。

sales@spasvo.com