标签:show tar else 技术分享 star void ble 代码 highlight
实现效果:
知识运用:
ServiceController组件的MachineName属性 //获取或设置服务所驻留的计算机名称
public string MachineName{get;set;} //属性值:与此ServiceController实例关联的服务的计算机名称
ServiceName属性 //获取或设置对此实例引用的服务进行标识的名称
public string ServiceName{get;set;}
Status属性 //获取由此实例引用的服务的状态
public ServiceControllerStatus Status {get;} //枚举值之一
DisplayName属性 //获取或设置服务的友好名称
public string DisplayName { get;set; }
CanStop属性 //指示服务在启动后是否可以停止
public bool CanStop { get; }
Start Stop方法
实现代码:
//启动服务 private void btn_start_Click(object sender, EventArgs e) { serviceController1.MachineName="."; serviceController1.ServiceName="wuauserv"; if (serviceController1.Status == ServiceControllerStatus.Running) { MessageBox.Show( serviceController1.DisplayName+"服务正在运行"); Application.Exit(); } else { serviceController1.Start(); MessageBox.Show( serviceController1.DisplayName+"服务启动成功"); btn_start.Enabled = false; Application.Exit(); } } //判断状态 private void btn_status_Click(object sender, EventArgs e) { try { serviceController1.MachineName = "."; serviceController1.ServiceName = "wuauserv"; if (serviceController1.Status == ServiceControllerStatus.Running) { MessageBox.Show(serviceController1.DisplayName + "服务正在运行"); btn_stop.Enabled = true; btn_status.Enabled = false; } else { MessageBox.Show(serviceController1.DisplayName + "服务已经停止"); btn_start.Enabled = true; btn_status.Enabled = false; } } catch (Exception ee) { MessageBox.Show(ee.Message); } } //停止服务 private void btn_stop_Click(object sender, EventArgs e) { try { serviceController1.MachineName = "."; serviceController1.ServiceName = "wuauserv"; if (serviceController1.CanStop) { serviceController1.Stop(); MessageBox.Show(serviceController1.DisplayName+"服务已经停止"); Application.Exit(); } else { MessageBox.Show("服务不可以停止"); Application.Exit(); } } catch (Exception ee) { MessageBox.Show(ee.Message); } }
标签:show tar else 技术分享 star void ble 代码 highlight
原文地址:https://www.cnblogs.com/feiyucha/p/10199544.html