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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Linux System Programming note 8 ——File and Directory Management

    Linux System Programming note 8 ——File and Directory Management

    2022年1月1日
    45
  • StrictMode使用

    StrictMode使用【IT168技术】最新的Android平台中(Android2.3起),新增加了一个新的类,叫StrictMode(android.os.StrictMode)。这个类可以用来帮助开发者改进他们编写的应用,并且提供了各种的策略,这些策略能随时检查和报告开发者开发应用中存在的问题,比如可以监视那些本不应该在主线程中完成的工作或者其他的一些不规范和不好的代码。  StrictMode有多种不

    2022年6月10日
    50
  • swift 语言获取触摸点坐标 touchesBegan 中的 touches的坐标获取「建议收藏」

    swift 语言获取触摸点坐标 touchesBegan 中的 touches的坐标获取「建议收藏」overridefunctouchesBegan(touches:Set,withEventevent:UIEvent){    varp=touches.anyObject().locationInview(self)  }很多老教材都是这个方法来获取,touches.anyObject(),可是最新的版本提示touches根本没有anyObject

    2022年7月25日
    13
  • 并查集基础总结

    并查集基础总结

    2021年9月27日
    52
  • VHDL与Verilog的混合设计[通俗易懂]

    VHDL与Verilog的混合设计[通俗易懂]VHDL调用Verilog模块的时候,要在实例化模块前,加上“verilogmodelGM:”VHDL调用verlogverilogmodule:modulem(a,b,c);inputa,b;outputc;…endmodule调用如下:compoentmport(a:instd_logic;…

    2022年9月18日
    2
  • elemtype到底是个啥?

    elemtype到底是个啥?以前对这个东西的一知半解,今天有时间,查了多方面的资料,总结下:ElemType简单来说就是:用来更好的替代,他也可以叫做别的名字,比如说:#defineElemTypeint写程序,就可以用ElemType来进行替代int,若以后想要改Elemtype所定义的数据类型为char,直接#defineElemTypechar只要是其涉及到的全部修改了数据类型,可以修改最少量的代码,…

    2022年5月12日
    103

发表回复

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

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