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


相关推荐

  • tess4j验证码识别

    tess4j验证码识别tess4j的安装和使用参考:https://www.cnblogs.com/cmyxn/p/6993422.htmltess4j提高识别率1.对称近邻均值滤波参考:http://blog.csdn.net/fangbinwei93/article/details/505624492.指定config为digits,并修改tessdata\configs\digits文件,将白名单中设置…

    2022年6月12日
  • mysql添加字段的sql语句_mysql增加字段的语句

    mysql添加字段的sql语句_mysql增加字段的语句myesl是一个关系型数据库管理系统,也是目前最流行的管理系统之一,目前网页设计方面对一个系统的使用频率是比较高的,因此想要学会网页设计,那么有关myesl的基础知识以及应用就一定要掌握。而今天我们就来看一看myesl增加字段是如何操作的。mysql增加字段——mysql数据库更新字段语句一、UPDATE:UPDATE的功能是更新表中的数据。这的语法和INSERT的第二种用法相似。必须提供表名以及…

    2022年10月26日
  • RapeLay(电车之狼R)的结局介绍 (隐藏结局攻略)

    RapeLay(电车之狼R)的结局介绍 (隐藏结局攻略)

    2021年12月7日
  • python进制转换函数

    python进制转换函数一:二,八,十六进制转十进制注意2进制对应的数值范围只能是0和1,超过范围会报错,8进制和16进制同理。print(int(‘100′,2))#二进制转十进制,int(’20’,2)会报错print(int(’30’,8))#八进制转十进制,int(’80’,8)会报错print(int(‘f0’,16))#十六进制转十进制,int(‘g0’,16)会报错二:十进制转二进制、八进制、十六进制。内置函数bin、oct、hex得到的进制前面会分别带有’0b’,‘0o’,’0x’字符。

    2022年5月12日
  • 惠普电脑如何设置u盘启动_惠普笔记本电脑怎么用u盘重装系统

    惠普电脑如何设置u盘启动_惠普笔记本电脑怎么用u盘重装系统惠普笔记本现在算是比较普遍,很多用户都会想给自己的惠普笔记本重装系统,那么惠普怎么重装系统呢?下面介绍一下惠普笔记本u盘系统安装步骤。惠普u盘启动系统安装步骤阅读1、将U盘插在USB接口,开机并不断按下启动U盘快捷键。2、在进入系统启动菜单中选择有USB字样的选项并回车。3、系统启动后会进入PE界面,老机型选择Win2003PE,选择完后回车进入。4、等待进入系统后,无需操作,云骑士会自动打开进行…

    2022年8月13日
  • 几款主流好用的markdown编辑器介绍

    几款主流好用的markdown编辑器介绍Markdown编辑器 Markdown其实在2004年就有了,不过之前一直很小众,这几年随着相关应用平台的发展,Markdown以其独到的优势迅速火起来了。Markdown编辑器使用一套格式标记语言来对文档内容进行排版和格式显示。而Markdown的标记很少,常用的也就十个左右,它是一种非常轻量的标记语言。 Markdown编辑器深受技术人员的喜爱,Markdown可以用来编写说明…

    2022年4月29日

发表回复

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

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