C#使用ServiceController控制windows服务
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服务的更多相关文章
- C# ASP.NET 控制windows服务的 开启和关闭 以及重启
用ASP.NET控制Windows服务的开启与关闭效果如图 代码 首页页面需要添加引用 页面的pageload中 实例化windows服务 protected void Page_Load(objec ...
- win7 提升windows服务权限使非管理员用户可以控制windows服务的开启和关闭
#include <windows.h>#include <tchar.h>#include <strsafe.h>#include <aclapi.h> ...
- 使用WMI控制Windows进程 和服务
1.使用WMI控制Windows进程 本文主要介绍两种WMI的进行操作:检查进程是否存在.创建新进行 代码如下: using System; using System.Collections.Gene ...
- windows服务
.net windows 服务创建.安装.卸载和调试 原文:http://www.cnblogs.com/hfliyi/archive/2012/08/12/2635290.html 我对例子做了 ...
- .net windows 服务创建、安装、卸载和调试
原文:http://blog.csdn.net/angle860123/article/details/17375895 windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境 ...
- C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(上)
译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(上)),不对的地方欢迎指出与交流. 章节出自<Professional C ...
- C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(下)
译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(下)),不对的地方欢迎指出与交流. 章节出自<Professional C ...
- C#开发Windows服务详细流程
1.Windows服务简单介绍 Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序,主要用于长时间运行的功能或者执行定时任务.一般情况下,用户不能通过用户界面来安装和启 ...
- 创建Windows服务
windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志.计算机启动时,服务会自动开始 ...
随机推荐
- POJ 1287 Networking(最小生成树)
题意 给你n个点 m条边 求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algor ...
- Android实现限制EditText输入文字的数量
一: 声明控件. TextView hasnumTV; TextView hasnum;// 用来显示剩余字数 int num = 50;// 限制的 ...
- 【python下使用OpenCV实现计算机视觉读书笔记4】保存摄像头视频
读取摄像头内容,然后保存一段十秒钟的视频. import cv2 cameraCapture = cv2.VideoCapture(0) fps = 30 # an assumption size = ...
- import android.provider.Telephony cannot be resolved
android.provider.Telephony is hidden. http://androidxref.com/4.0.3_r1/xref/frameworks/base/core/java ...
- 开源Math.NET基础数学类库使用(17)C#计算矩阵条件数
原文:[原创]开源Math.NET基础数学类库使用(17)C#计算矩阵条件数 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...
- acdream 1211 Reactor Cooling 【边界网络流量 + 输出流量】
称号:acdream 1211 Reactor Cooling 分类:无汇的有上下界网络流. 题意: 给n个点.及m根pipe,每根pipe用来流躺液体的.单向的.每时每刻每根pipe流进来的物质要等 ...
- Android架构分析之LOG模块
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android版本:2.3.7_r1 Linux内核版本:android-goldfish-2.6.29 Andro ...
- [TWRP 2.8.4 ] 小米 3W 中文-英文版本 twrp
经过半个小时的代码修改,从 2.7.0 移植到 2.8.4 的源代码上. 汉化操作是使用切换语言选项来实现的. 注意: 此版本的 twrp 还不支持双系统切换功能. 作者:laser杨万荣 如果需要转 ...
- 【原创】纯OO:从设计到编码写一个FlappyBird (四)
第三部分请点这里 这里来实现Obstacle类.其实flappybird的本质就是小鸟原地掉,然后几根柱子在走.这也是在Game类里,用obs.move()来实现游戏逻辑的原因. 我们首先必须确定几个 ...
- ShareSDK for Android 2.3.8它已发表
ShareSDK for Android 2.3.8已经公布,本次更新内容包含: 1.一键分享加入"摇一摇截图分享"功能 3.优化一键分享截图分享功能 4.一键分享编辑页界面微调 ...