C#使用ServiceController控制windows服务

C#使用ServiceController控制windows服务

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

    C#在,使用ServiceController控制类windows服务,添加首次使用前引文:System.ServiceProcess,空间中引用:using System.ServiceProcess。

以下举例获取本机的全部已安装的Windows服务和应用,然后查找某一应用活服务是否已经安装。

C#使用ServiceController控制windows服务

代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.ServiceProcess;namespace 推断机器中是否安装了某项服务或者应用{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        ServiceController[] Services = ServiceController.GetServices();        private bool ExistSth()        {            bool exist = false;            for (int i = 0; i < Services.Length; i++)            {                if (Services[i].DisplayName.ToString() == textBox1.Text.Trim())                    exist = true;              }            return exist;        }        private void button1_Click(object sender, EventArgs e)        {            if (ExistSth())                MessageBox.Show("已安装");            else                MessageBox.Show("未安装");        }        private void Form1_Load(object sender, EventArgs e)        {            for (int i = 0; i < Services.Length; i++)                listBox1.Items.Add(Services[i].DisplayName.ToString());        }    }}


       如果某一服务名为ServicesName, 编写開始服务,停止服务,重新启动服务的代码例如以下:

private ServiceController _controller;private void StopService(){    this._controller = new ServiceController("ServicesName");    this._controller.Stop();    this._controller.WaitForStatus(ServiceControllerStatus.Stopped);    this._controller.Close();}private void StartService(){    this._controller = new ServiceController("ServicesName");    this._controller.Start();    this._controller.WaitForStatus(ServiceControllerStatus.Running);    this._controller.Close();}private void ResetService(){    this._controller = new ServiceController("ServicesName");    this._controller.Stop();    this._controller.WaitForStatus(ServiceControllerStatus.Stopped);    this._controller.Start();    this._controller.WaitForStatus(ServiceControllerStatus.Running);    this._controller.Close();}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117255.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java设计模式之结构型:享元模式

    Java设计模式之结构型:享元模式

    2021年10月4日
    41
  • RPC接口设计_java rpc项目

    RPC接口设计_java rpc项目写在前边分布式架构是互联网应用的基础架构,很多新人入职以来就开始负责编写和调用阿里的各种远程接口。但如同结婚一般,用对一个正确的接口就如同嫁一个正确的人一样,往往难以那么顺利的实现,或多或少大家都会在这个上边吃亏。每年双十一系统调用复盘的时候,我都会听到以下声音你们调我的接口报错了竟然不会自己重试?我的返回值应该从这里取我返回isSuccess()==true,不代表业务成功,你…

    2022年10月13日
    0
  • SQL Developer_Netbank

    SQL Developer_NetbankOracle .Net Develoer

    2022年4月22日
    46
  • sp_executesql 与 参数

    sp_executesql 与 参数总结了一下 sp_executesql 与 参数 的关系 sp_executesql  并不能通过参数列表指定任意部分,在普通sql语句中是变量的可以指定,是常量的不能指定。在sp_executesql 执行的字符串中, 下面称为spStr,有些是在设置sql字符串前就必须指定的,有些是以变量的形式指定的。跟sql语句相一致,这里语句称为 sqlStr,凡是在sqlStr语句中必须要声明为常

    2022年5月21日
    30
  • stringutil.isnotempty_中低腰和低腰的区别

    stringutil.isnotempty_中低腰和低腰的区别学习中遇到了这个地方,搜了一下,这位仁兄总结的挺详细,就粘了过来原文链接(https://www.cnblogs.com/dixinyunpan/p/6088612.html)isNotEmpty(str)等价于str!=null&&str.length>0isNotBlank(str)等价于str!=null&&str…

    2022年8月12日
    3
  • 成长之路——InfoQ视频心得笔记[通俗易懂]

    这期是普元信息的主任架构师,顾伟! 视频地址: http://mp.weixin.qq.com/s/0KE_CCU3cWwzvr7D5ENtsQ少年,在路上!不卑不亢!!!1:换维思考 墨菲定律:如果有两种或两种以上的方式去做某件事情,而其中一种选择方式将导致灾难,则必定有人会做出这种选择。彼得定律:向上爬的定律!错误和面对压力方面的表现!犯错:积累沉淀,不要犯相同的错误两次!2:人

    2022年2月25日
    33

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号