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

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

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHVja3k1MTIyMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

代码:

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();
}

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

C#使用ServiceController控制windows服务的更多相关文章

  1. C# ASP.NET 控制windows服务的 开启和关闭 以及重启

    用ASP.NET控制Windows服务的开启与关闭效果如图 代码 首页页面需要添加引用 页面的pageload中 实例化windows服务 protected void Page_Load(objec ...

  2. win7 提升windows服务权限使非管理员用户可以控制windows服务的开启和关闭

    #include <windows.h>#include <tchar.h>#include <strsafe.h>#include <aclapi.h> ...

  3. 使用WMI控制Windows进程 和服务

    1.使用WMI控制Windows进程 本文主要介绍两种WMI的进行操作:检查进程是否存在.创建新进行 代码如下: using System; using System.Collections.Gene ...

  4. windows服务

    .net windows 服务创建.安装.卸载和调试   原文:http://www.cnblogs.com/hfliyi/archive/2012/08/12/2635290.html 我对例子做了 ...

  5. .net windows 服务创建、安装、卸载和调试

    原文:http://blog.csdn.net/angle860123/article/details/17375895 windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境 ...

  6. C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(上)

    译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(上)),不对的地方欢迎指出与交流. 章节出自<Professional C ...

  7. C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(下)

    译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(下)),不对的地方欢迎指出与交流. 章节出自<Professional C ...

  8. C#开发Windows服务详细流程

    1.Windows服务简单介绍 Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序,主要用于长时间运行的功能或者执行定时任务.一般情况下,用户不能通过用户界面来安装和启 ...

  9. 创建Windows服务

    windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志.计算机启动时,服务会自动开始 ...

随机推荐

  1. oracle nologging用法(转)

    一.oracle日志模式分为(logging,force logging,nologging) 默认情况是logging,就是会记录到redo日志中,force logging是强制记录日志,nolo ...

  2. Leetcode_191_Number of 1 Bits

    本文是在学习中的总结.欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/44486547 Write a function that ...

  3. 【Cocos2d-x 粒子系统】火球用手指飞起来

    程序: 创建球 sp1 = CCSprite::create("ball.png"); sp1->setPosition(ccp(10, visibleSize.height ...

  4. SVM-SVM概述

    (一)SVM背景资料简介 支持向量机(Support Vector Machine)这是Cortes和Vapnik至1995首次提出,样本.非线性及高维模式识别中表现出很多特有的优势,并可以推广应用到 ...

  5. C#中使用gRPC

    C#中使用gRPC 我的这几篇文章都是使用gRPC的example,不是直接编译example,而是新建一个项目,从添加依赖,编译example代码,执行example.这样做可以为我们创建自己的项目 ...

  6. JS脚本加载与执行对性能的影响

    高性能JavaScript-JS脚本加载与执行对性能的影响 在web产品优化准则中,很重要的一条是针对js脚本的加载和执行方式的优化.本篇文章简单描述一下其中的优化准则. 1. 脚本加载优化 1.1 ...

  7. 基于Cocos2dx开发卡牌游戏_松开,这三个国家

    1.它实现了动态读取地图资源.地图信息被记录excel桌格.假设你想添加地图,编者excel导入后CocoStudio数据编辑器,然后出口到Json档,到项目的Resource文件夹下. 2.SGFi ...

  8. TaintDroid:智能手机监控实时隐私信息流跟踪系统(一)

    1.1     摘要 现今,智能手机操作系统不能有效的提供给用户足够的控制权并且很清楚的了解到第三方的应用程序是如何使用其的隐私数据.我们使用了TaintDroid来阐明这个缺点,其是一个高效的,全系 ...

  9. 基数排序---Java实现+C++实现

    基数排序是基于桶排序实现的,总之基本思想是:先基于个位进行桶排序,更新原序列:再基于十位进行桶排序,更新原序列-- code1:java import java.util.*; public clas ...

  10. CLR Profile解决内存占用过高

    CLR Profile解决内存占用过高的问题 炮哥:"嘿,哥们,忙啥呢,电脑卡成这逼样." 勇哥:"在用CLR Profile工具分析下FlexiPrint的内存占用情况 ...