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 1146:ID Codes
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description ...
- 第三章 AOP 基于Schema的AOP
基于Schema定义的切面和前现两种方式定义的切面,内容上都差不多,只是表现形式不一样而已. 3.7.1一般增强的使用 a.目标类 public class Target { public void ...
- SQL Server :理解DCM页
原文:SQL Server :理解DCM页 我们已经讨论了各种不同的页,包括数据页.GAM与SGAM页.PFS页,还有IAM页.今天我们来看下差异变更页(Differential Change Map ...
- C语言 cgi(3)
1cs3157 – Advanced ProgrammingSummer 2014, Project 1, 150 pointsJune 17, 2014Follow these step-by-st ...
- 批处理bat脚本编写(附详细例子)
批处理bat脚本编写(附详细例子) 由于在项目开发的过程中经常需要编写bat脚本,而看大牛们编写 ...
- PL SQLDEVELOPMENT导出数据库脚本
Tools--export Tables--选择表--SQL Inserts-- watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHprMTU2MjExMD ...
- rsync 只是测试,请看下一篇
实现从客户服务器去同步资源服务器 1.解压 # tar -xzpvf rsync-2.5.6.tar.gz 编译安装 # cd rsync-2.5.6/ # ./configure --pref ...
- Java Swing 树状组件JTree的使用方法(转)
树中特定的节点可以由 TreePath(封装节点及其所有祖先的对象)标识,或由其显示行(其中显示区域中的每一行都显示一个节点)标识.展开 节点是一个非叶节点(由返回 false 的 TreeModel ...
- OWIN 为WebAPI
OWIN 为WebAPI 宿主 跨平台 OWIN是什么? OWIN的英文全称是Open Web Interface for .NET. 如果仅从名称上解析,可以得出这样的信息:OWIN是针对.NET平 ...
- mybatis至mysql插入一个逗号包含值误差
mybatis至mysql插入形如"11,22,33"当误差.我使用了错误的原因是美元符号镶嵌sql.正确的做法是使用# 有时间去看看mybatis的$和#差异. 版权声明:本文 ...