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)
上一篇 2022年1月8日 下午4:00
下一篇 2022年1月8日 下午4:00


相关推荐

  • 一文说清楚pytorch和tensorFlow的区别究竟在哪里

    最近用了一点pytorch,想着稍稍理一下,这样一个和TensorFlow抗衡的一个框架,究竟是何方神圣?首先我们要搞清楚pytorch和TensorFlow的一点区别,那就是pytorch是一个动态的框架,而TensorFlow是一个静态的框架。何为静态的框架呢?我们知道,TensorFlow的尿性是,我们需要先构建一个TensorFlow的计算图,构建好了之后,这样一个计算图是不能够变的了…

    2022年4月5日
    102
  • loadrunner11 post请求接口压力测试并生成报告「建议收藏」

    loadrunner11 post请求接口压力测试并生成报告「建议收藏」以管理员身份运行loadRunner,进行脚本编写新建脚本选择http请求默认弹出窗口直接关闭就可以选泽scriot-点击action-输入执行脚本Action(){web_custom_request(“222″,”URL=http://10.129.2.121:8082/api/vehicle_license/base64_image”,”Method=POST”,

    2022年7月17日
    44
  • ElasticSearch数据库的使用简介

    ElasticSearch数据库的使用简介elasticsearc 使用简介

    2026年3月18日
    2
  • C语言 stat 函数「建议收藏」

    C语言 stat 函数「建议收藏」C语言stat函数stat头文件:#include<sys/stat.h>#include<unistd.h>函数原型:intstat(constchar*file_name,structstat*buf)**函数说明:**stat函数获取file_name指向文件的文件状态,并将文件信息保存到结构体buf中,执行成功返回0,失…

    2022年8月21日
    22
  • 主从复制、读写分离、集群、为什么要使用Redis数据库[通俗易懂]

    主从复制、读写分离、集群、为什么要使用Redis数据库[通俗易懂]一、什么是主从复制、读写分离、为什么要使用主从复制:是一种数据备份的方案。简单来说,是使用两个或两个以上相同的数据库,将一个数据库当做主数据库,而另一个数据库当做从数据库。在主数据库中进行相应操作时,从数据库记录下所有主数据库的操作,使其二者一模一样。读写分离:是一种让数据库更稳定的的使用数据库的方法。是在有从数据库的情况下使用,当主数据库进行对数据的增删改也就是写操作时,将查询的…

    2022年6月13日
    26
  • python获得当前时间戳_python怎么获取当前时间戳

    python获得当前时间戳_python怎么获取当前时间戳python 获取当前时间戳的方法 1 使用 time 模块 语法为 time time 2 使用 datetime 模块 语法为 datetime datetime now timestamp 使用模块 timeimportti time time print now 使用模块 datetime 模块 datetime 提供了以更面向对象的方式操作

    2026年3月17日
    2

发表回复

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

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