C#中调用Windows系统服务exe程序的工具类与重启服务的流程
场景
使用C#编写的Windows服务程序,在Winform中进行调用。
常用工具类方法检测服务是否存在或者安装,获取服务状态,启动服务,停止服务的方法。
以在Winform中重启服务为例。
注:
博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载
实现
新建工具类WinServiceHelper
检测服务是否安装或者存在的方法
/// <summary>
/// 服务是否安装/存在
/// </summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public static bool IsServiceInstalled(string serviceName)
{
bool exists = false;
System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();
foreach (System.ServiceProcess.ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
exists = true;
break;
}
}
return exists;
}
获取服务状态的方法
/// <summary>
/// 获取服务状态
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
public static String GetServiceStatus(string serviceName)
{
string result = "服务不存在";
System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();
foreach (System.ServiceProcess.ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
result = s.Status.ToString();
break;
}
}
return result;
}
注:
服务状态返回值是枚举类型,具体返回值如下
// 摘要:
// 指示服务的当前状态。
public enum ServiceControllerStatus
{
// 摘要:
// 服务未运行。这对应于 Win32 SERVICE_STOPPED 常数,该常数定义为 0x00000001。
Stopped = ,
//
// 摘要:
// 服务正在启动。这对应于 Win32 SERVICE_START_PENDING 常数,该常数定义为 0x00000002。
StartPending = ,
//
// 摘要:
// 服务正在停止。这对应于 Win32 SERVICE_STOP_PENDING 常数,该常数定义为 0x00000003。
StopPending = ,
//
// 摘要:
// 服务正在运行。这对应于 Win32 SERVICE_RUNNING 常数,该常数定义为 0x00000004。
Running = ,
//
// 摘要:
// 服务即将继续。这对应于 Win32 SERVICE_CONTINUE_PENDING 常数,该常数定义为 0x00000005。
ContinuePending = ,
//
// 摘要:
// 服务即将暂停。这对应于 Win32 SERVICE_PAUSE_PENDING 常数,该常数定义为 0x00000006。
PausePending = ,
//
// 摘要:
// 服务已暂停。这对应于 Win32 SERVICE_PAUSED 常数,该常数定义为 0x00000007。
Paused = ,
}
启动服务的方法
/// <summary>
/// 启动服务
/// </summary>
/// <param name="serivceExeFullPath">服务全路径</param>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public static bool ServiceStart(string serivceExeFullPath ,string serviceName)
{
if (!IsServiceInstalled(serviceName))
{
MessageBox.Show("服务未安装,请先安装!");
return false;
}
try
{
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = serivceExeFullPath;
p.StartInfo.Arguments = "start";
p.Start();
p.Close();
}
System.Threading.Thread.Sleep();
return true;
}
catch (Exception ex)
{
MessageBox.Show("服务安装异常:" + ex.Message);
return false;
}
}
停止服务的方法
/// <summary>
/// 停止服务
/// </summary>
/// <param name="serivceExeFullPath">服务全路径</param>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public static bool ServiceStop(string serivceExeFullPath, string serviceName)
{
if (!IsServiceInstalled(serviceName))
{
MessageBox.Show("服务未安装,请先安装!");
return false;
}
try
{
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = serivceExeFullPath;
p.StartInfo.Arguments = "stop";
p.Start();
p.WaitForExit();
p.Close();
}
System.Threading.Thread.Sleep();
return true;
}
catch (Exception ex)
{
MessageBox.Show("服务停止异常:" + ex.Message);
return false;
}
}
重启服务示例
在重启服务的按钮的点击事件中
//检测服务是否安装
bool isInstalled = WinServiceHelper.IsServiceInstalled(Global.BTS_DATA_SERVICE_NAME);
if (!isInstalled)
{
MessageBox.Show("重启失败,服务"+Global.BTS_DATA_SERVICE_NAME+"未安装或未启动");
return;
}
string serviceStatus = WinServiceHelper.GetServiceStatus(Global.BTS_DATA_SERVICE_NAME);
if (!serviceStatus.Equals(System.ServiceProcess.ServiceControllerStatus.Running.ToString()))
{
MessageBox.Show("重启失败,服务" + Global.BTS_DATA_SERVICE_NAME + "状态为:" + serviceStatus);
return;
}
string serivceExeFullPath = Global.AppConfig.BtsDataServiceExe;
string serviceName = Global.BTS_DATA_SERVICE_NAME;
bool isStopSuccess = WinServiceHelper.ServiceStop(serivceExeFullPath,serviceName);
//停止失败
if (!isStopSuccess)
{
MessageBox.Show("重启失败,服务" + Global.BTS_DATA_SERVICE_NAME + "停止失败");
return;
}
//方法里已经休眠2秒
bool isStartSuccess = WinServiceHelper.ServiceStart(serivceExeFullPath, serviceName);
//启动失败
if (!isStartSuccess)
{
MessageBox.Show("重启失败,服务" + Global.BTS_DATA_SERVICE_NAME + "启动失败");
return;
}
MessageBox.Show("服务" + Global.BTS_DATA_SERVICE_NAME + "重启成功");
C#中调用Windows系统服务exe程序的工具类与重启服务的流程的更多相关文章
- 在C#中调用另一个应用程序或命令行(.exe 带参数)<zz>
在.net中使用system.diaglostics.Process可以用来调用另一个命令行或程序. using System.Diagnostics; 如果是dos Proces ...
- C#中调用Windows API的要点 .
介绍 API(Application Programming Interface),我想大家不会陌生,它是我们Windows编程的常客,虽然基于.Net平台的C#有了强大的类库,但是,我们还是不能否认 ...
- Unity中调用Windows窗口句柄以及根据需求设置并且解决扩展屏窗体显示错乱/位置错误的Bug
问题背景: 现在在搞PC端应用开发,我们开发中需要调用系统的窗口以及需要最大化最小化,缩放窗口拖拽窗口,以及设置窗口位置,去边框等功能 解决根据: 使用user32.dll解决 具体功能: Unity ...
- C#中调用Windows API时的数据类型对应关系
原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE=System.UInt16 CHAR=System. ...
- java调用kettle的job和transfer工具类
package com.woaiyitiaocai.util; import java.util.Map; import java.util.UUID; import org.apache.log4j ...
- 如何在Processing中调用Windows应用程序
Processing调用了exe就意味着失去了跨平台.调用的过程是,先得到当前的runtime,然后调用runtime的exec()方法,在exec()传入的是字符串参数,这个参数很重要,该有空格的地 ...
- Silverlight调用本机exe程序
要点: 1. Silverlight必须启用OOB模式,以及 Require elevated trust when running in-browser.参考下图设置 注:OOB模式,并不意味着必须 ...
- EasyNVR无插件H5/HLS/m3u8直播解决方案中Windows系统服务启动错误问题的修复:EasyNVR_Service 服务因 函数不正确。 服务特定错误而停止。
最近在做某地市移动公司景观直播的项目时,遇到一个问题,当我们部署EasyNVR为系统服务后,居然出现了无法启动服务的现象,表面上看,提示是系统服务启动失败,实际通过查看windows 系统日志: 查找 ...
- SEXTANTE中调用任意C++控制台程序的简单例子
在sextante中单纯利用python或者调用sextante已有算法进行自定义开发,很多情况下速度不咋给力,同样的操作调用QGIS的C++插件比用sextante里的算法要快,有时候快的 还不止一 ...
随机推荐
- SpringBoot项目版本升级:从1.5.3升级到2.1.8版本
SpringBoot项目版本升级:从1.5.3升级到2.1.8版本 前言 简单记录一次本人在自己的SpringBoot项目project-template中,把1.5.3版本升级到2.1.8版本时升级 ...
- 《Python学习手册 第五版》 -第6章 动态类型
本章主要讲述变量.对象.引用三者直接的关联及区别,详细说明了在变量赋值的操作中,计算机内部到底发生了什么,有哪些是不被人察觉和需要明确了解的 1.先从最简单的赋值语句开始 a=3 这一句,基本就能涵盖 ...
- 转载 angularJS filter 过滤器
angularjs中的filter(过滤器) 标签: angularjsfilter 源文地址:http://www.ncloud.hk/技术分享/angularjs中的filter-过滤器/ f ...
- C++解析Json,使用JsonCpp读写Json数据
JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.通常用于数据交换或存储. JsonCpp是一个基于C++语言的开源库,用于C++程序的J ...
- RFC笔记—Neighbor Discovery for IP version 6 (IPv6)
Router Solicitation Message Source Address An IP address assigned to the sending interface, or the u ...
- [Python]pip 国内源
临时使用方法 pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com celery END
- File类和枚举
java.io.File类:文件和目录路径名的抽象表示形式 File类常见构造方法: File(String pathname):通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例. ...
- go每日新闻--2020-02-27
go 语言中文网(每日资讯)_2020-02-27 一.Go 语言中文网 如何正确看待 Google 宣布 Fuchsia 操作系统没有选 Go 作为终端开发语言 Actor 还是 CSP?Go 中的 ...
- codewars--js--Pete, the baker
问题描述: Pete likes to bake some cakes. He has some recipes and ingredients. Unfortunately he is not go ...
- Re:连点器
连点器 介绍 顾名思义,可以连续点的机器. 当然,连续可快可慢:机器意味着不许要人工点击:可以是生活中的机器,也可以是电脑中的程序. 现在,连点器网上一搜一大堆,什么鼠标连点精灵,鼠大侠……不仅有电脑 ...