C# Window Service安装、卸载、恢复选项操作
using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
namespace ScmWrapper
{
public class ServiceHandler
{
#region 安装服务
/// <summary>
/// 安装服务
/// </summary>
public static bool InstallService(string nameService, string serviceFileName)
{
bool flag = true;
if (!IsServiceIsExisted(nameService))
{
try
{
using (Process myPro = new Process())
{
myPro.StartInfo.FileName = "cmd.exe";
myPro.StartInfo.UseShellExecute = false;
myPro.StartInfo.RedirectStandardInput = true;
myPro.StartInfo.RedirectStandardOutput = true;
myPro.StartInfo.RedirectStandardError = true;
myPro.StartInfo.CreateNoWindow = true;
myPro.Start();
//如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
string str = $"{serviceFileName} install &exit";
myPro.StandardInput.WriteLine(str);
myPro.StandardInput.AutoFlush = true;
myPro.WaitForExit();
}
}
catch
{
flag = false;
}
//try
//{
// InstallmyService(null, serviceFileName);
//}
//catch (Exception ex)
//{
// flag = false;
//}
}
return flag;
}
#endregion
#region 卸载服务
/// <summary>
/// 卸载服务
/// </summary>
public static bool UninstallService(string nameService, string serviceFileName)
{
bool flag = true;
if (IsServiceIsExisted(nameService))
{
if (IsServiceStart(nameService))
{
StopService(nameService);
}
try
{
using (Process myPro = new Process())
{
myPro.StartInfo.FileName = "cmd.exe";
myPro.StartInfo.UseShellExecute = false;
myPro.StartInfo.RedirectStandardInput = true;
myPro.StartInfo.RedirectStandardOutput = true;
myPro.StartInfo.RedirectStandardError = true;
myPro.StartInfo.CreateNoWindow = true;
myPro.Start();
//如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
string str = $"{serviceFileName} uninstall &exit";
myPro.StandardInput.WriteLine(str);
myPro.StandardInput.AutoFlush = true;
myPro.WaitForExit();
}
}
catch
{
flag = false;
}
//try
//{
// UnInstallmyService(serviceFileName);
//}
//catch
//{
// flag = false;
//}
}
return flag;
}
#endregion
#region 检查服务存在的存在性
/// <summary>
/// 检查服务存在的存在性
/// </summary>
/// <param name=" NameService ">服务名</param>
/// <returns>存在返回 true,否则返回 false;</returns>
public static bool IsServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
bool exist = services.Where(n => n.ServiceName.Equals(serviceName, StringComparison.OrdinalIgnoreCase))?.Count() > 0;
services = null;
return exist;
}
#endregion
#region 判断window服务是否启动
/// <summary>
/// 判断某个Windows服务是否启动
/// </summary>
/// <returns></returns>
public static bool IsServiceStart(string serviceName)
{
ServiceController psc = new ServiceController(serviceName);
bool bStartStatus = false;
try
{
if (!psc.Status.Equals(ServiceControllerStatus.Stopped))
{
bStartStatus = true;
}
return bStartStatus;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
psc.Close();
psc.Dispose();
}
}
#endregion
#region 修改服务的启动项
public static void SetRecoveryOptions(string serviceName)
{
try
{
using (Process myPro = new Process())
{
myPro.StartInfo.FileName = "cmd.exe";
myPro.StartInfo.UseShellExecute = false;
myPro.StartInfo.RedirectStandardInput = true;
myPro.StartInfo.RedirectStandardOutput = true;
myPro.StartInfo.RedirectStandardError = true;
myPro.StartInfo.CreateNoWindow = true;
myPro.Start();
//如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
string str = $"sc failure GeoFence reset=0 actions=restart/60000/restart/60000/restart/60000 &exit";
myPro.StandardInput.WriteLine(str);
myPro.StandardInput.AutoFlush = true;
myPro.WaitForExit();
}
}
catch
{
}
}
#endregion
#region 启动服务
public static bool StartService(string serviceName)
{
bool flag = true;
if (IsServiceIsExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
{
service.Start();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
break;
}
if (i == 59)
{
flag = false;
}
}
}
service.Close();
service.Dispose();
}
return flag;
}
#endregion
#region 停止服务
public static bool StopService(string serviceName)
{
bool flag = true;
if (IsServiceIsExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
service.Stop();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
break;
}
if (i == 59)
{
flag = false;
}
}
}
service.Close();
service.Dispose();
}
return flag;
}
#endregion
}
}
C# Window Service安装、卸载、恢复选项操作的更多相关文章
- windows service 安装/卸载
第一种方法: 前提: Service1 中的serviceProcessInstaller1设置 Account为localSystem 1. 开始 ->运行 ->cmd(管理员身份运行) ...
- Window Service安装不成功
1. 加Winsow Service 2. 加Setup Project Add -> Project Output , 选中Primary output from Winsow Serv ...
- Windows Service的安装卸载 和 Service控制(转)
Windows Service的安装卸载 和 Service控制 原文地址:http://www.cnblogs.com/Peter-Zhang/archive/2011/10/15/2212663. ...
- Windows Service的安装卸载 和 Service控制
原文 Windows Service的安装卸载 和 Service控制 本文内容包括如何通过C#代码安装Windows Service(exe文件,并非打包后的安装文件).判断Service是否存在. ...
- C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计
C#Windows Service服务程序的安装/卸载.启动/停止 桌面客户端管理程序设计 关于Windows Service程序的安装与卸载如果每次使用命令行操作,那简直要奔溃了,太麻烦而且还容易出 ...
- CentOS 7安装/卸载Redis,配置service服务管理
Redis简介 Redis功能简介 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 相比于传统的关系型数据库,Redis的存储方式是key-va ...
- window如何安装redis服务、卸载redis服务和启动redis服务
window如何安装redis服务.卸载redis服务和启动redis服务 一.总结 一句话总结:github上下载,解压,命令行运行(redis-server.exe redis.windows.c ...
- windows服务安装卸载
到C盘下找到对应的开发VS的installutil.exe文件,复制到程序的执行文件(*.exe)相同目录下在开始程序中找到VS命令提示工具 转到程序的执行文件(*.exe)目录下 C:\>cd ...
- 创建 window service 定时任务
参考文章:http://www.cnblogs.com/jack-liang/archive/2011/05/20/2051743.html 前段时间做过一个项目,前端系统提供添加定时任务,后端系统要 ...
随机推荐
- git命令详解( 六 )
此为git命令的第六篇 远程跟踪分支 不知道大家有没有发现在前面几篇中Git 好像知道 master 与 o/master 是相关的.当然这些分支的名字是相似的,可能会让你觉得是依此将远程分支 mas ...
- echart折线图系列一:折线图基本配置
引入echart插件 页面上准备一个容器:<div id="box" style="height:400px;width: 800px;padding: 20px& ...
- InfluxDB——python使用手册
InfluxDB--python使用手册 准备工作 安装InfluxDB: 请参考笔者相关博文:Centos7安装InfluxDB1.7 安装pip : yum install python-pip ...
- 潭州课堂25班:Ph201805201 tornado 项目 第十二课 项目部署(课堂笔记)
运行多个Tornado实例 网页响应不是特别的计算密集型处理 多个实例充分利用 CPU 多端口怎么处理 Linux 常见应用服务配置模式 nginx 和 supervisord:采用主配置文件 + 项 ...
- 2017-2018 Northwestern European Regional Contest (NWERC 2017)
A. Ascending Photo 贪心增广. #include<bits/stdc++.h> using namespace std; const int MAXN = 1000000 ...
- 创建phpinfo(PHP探针)查看自己服务器空间php详细信息
创建phpinfo(PHP探针)查看自己服务器空间php详细信息 <?phpphpinfo();?> 保存,然后更改文件名为phpinfo.php 放到你域名根目录,然后访问:http:/ ...
- golang str 首字母大写
首字母大写 //如果是小写字母, 则变换为大写字母 func strFirstToUpper(str string) string { if len(str) < 1 { return &quo ...
- 剑指offer——python【第23题】二叉搜索树的后序遍历序列
题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 解题思路 首先要清楚,这道题不是让你去判断一个给定 ...
- 修改MyEclipse字体大小及颜色
windows-->prefereces->General-->Appearance-->Colors and Fonts,在右边找到要修改的字体或背景,双击点Edit修改即可 ...
- SourceTree安装跳过登录
安装 SourceTree 时,需要使用atlassian授权,因为各种原因无法完成授权,现提供跳过 atlassian账号 授权方法. 安装之后,转到用户本地文件夹下的 SourceTree 目录, ...