Windows服务操作帮助类
/// <summary>
/// 打开系统服务
/// </summary>
/// <param name="serviceName">系统服务名称</param>
/// <returns></returns>
public static Tuple<bool, string> Open(string serviceName)
{
try
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status != ServiceControllerStatus.Running)
{
control.Start();
}
}
return new Tuple<bool, string>(true, "成功");
}
catch (Exception e)
{
return new Tuple<bool, string>(false, e.Message);
}
} /// <summary>
/// 关闭系统服务
/// </summary>
/// <param name="serviceName">系统服务名称</param>
/// <returns></returns>
public static Tuple<bool, string> Close(string serviceName)
{
try
{
using (ServiceController control = new ServiceController(serviceName))
{ if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
}
}
return new Tuple<bool, string>(true, "成功");
}
catch (Exception e)
{
return new Tuple<bool, string>(false, e.Message);
}
} /// <summary>
/// 重启系统服务
/// </summary>
/// <param name="serviceName">系统服务名称</param>
/// <returns></returns>
public static Tuple<bool, string> ReStart(string serviceName)
{
try
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Continue();
}
}
return new Tuple<bool, string>(true, "成功");
}
catch (Exception e)
{
return new Tuple<bool, string>(false, e.Message);
}
} /// <summary>
/// 返回服务状态
/// </summary>
/// <param name="serviceName">系统服务名称</param>
/// <returns>1:服务未运行 2:服务正在启动 3:服务正在停止 4:服务正在运行 5:服务即将继续 6:服务即将暂停 7:服务已暂停 0:未知状态</returns>
public static int GetSystemServiceStatus(string serviceName)
{
try
{
using (ServiceController control = new ServiceController(serviceName))
{
return (int)control.Status;
}
}
catch
{
return ;
}
} /// <summary>
/// 返回服务状态
/// </summary>
/// <param name="serviceName">系统服务名称</param>
/// <returns>1:服务未运行 2:服务正在启动 3:服务正在停止 4:服务正在运行 5:服务即将继续 6:服务即将暂停 7:服务已暂停 0:未知状态</returns>
public static string GetSystemServiceStatusString(string serviceName)
{
try
{
using (ServiceController control = new ServiceController(serviceName))
{
var status = string.Empty;
switch ((int)control.Status)
{
case :
status = "服务未运行";
break;
case :
status = "服务正在启动";
break;
case :
status = "服务正在停止";
break;
case :
status = "服务正在运行";
break;
case :
status = "服务即将继续";
break;
case :
status = "服务即将暂停";
break;
case :
status = "服务已暂停";
break;
case :
status = "未知状态";
break;
}
return status;
}
}
catch
{
return "未知状态";
}
} /// <summary>
/// 安装服务
/// </summary>
/// <param name="stateSaver"></param>
/// <param name="filepath"></param>
public static void Install(IDictionary stateSaver, string filepath)
{
try
{
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller
{
UseNewContext = true,
Path = filepath
};
myAssemblyInstaller.Install(stateSaver);
myAssemblyInstaller.Commit(stateSaver);
myAssemblyInstaller.Dispose();
}
catch (Exception ex)
{
throw new Exception("installServiceError/n" + ex.Message);
}
} /// <summary>
/// 判断服务是否存在
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
public static bool Existed(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
return services.Any(s => s.ServiceName == serviceName);
} /// <summary>
/// 卸载服务
/// </summary>
/// <param name="filepath">路径和文件名</param>
public static void UnInstall(string filepath)
{
try
{
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller
{
UseNewContext = true,
Path = filepath
};
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
}
catch (Exception ex)
{
throw new Exception("unInstallServiceError/n" + ex.Message);
}
}
Windows服务操作帮助类的更多相关文章
- Windows服务操作之sc和net命令及windows任务计划
看个粟子: 1.“新建项目”——“Window服务” 生成的目录结构 双击“MainService.cs”,右键点击“添加安装程序”,自动会生成Projectinstaller.cs文件以及两个安装组 ...
- Windows服务操作
资料 https://docs.microsoft.com/zh-cn/dotnet/api/system.serviceprocess.servicecontroller?redirectedfro ...
- C#.NET 操作Windows服务承载WCF
Windows服务的制作.安装可以参考这篇: C#.NET 操作Windows服务(安装.卸载) - runliuv - 博客园 (cnblogs.com) 本篇会在这个解决方案基础上,继续修改. 一 ...
- C#.NET 操作Windows服务(安装、卸载)
注意点: 1.安装时要请求到管理员权限. 2.卸载前,一定要停止掉Windows服务,否则需要重启或注销电脑.代码无法停止服务时,使用services.msc来停止. 开始: 1.新建一个名为&quo ...
- SC命令---安装、开启、配置、关闭 cmd命令行和bat批处理操作windows服务
一.cmd命令行---进行Windows服务操作 1.安装服务 sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32s ...
- cmd命令行和bat批处理操作windows服务(转载)
一.cmd命令行---进行Windows服务操作 1.安装服务 sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32srv ...
- 使用Visual Studio 2015 Community 开发windows服务
昨天研究在.NET下开发Windows服务程序,期间遇到一些小问题,这里仅将自己的开发过程和需要注意的地方写下和广大网友分享…… 1.基础 Windows服务是指系统启动时能够自己运行的程序.W ...
- 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 ...
随机推荐
- js关闭当前页面不弹出提示
window.top.opener=null; window.top.open('','_top');//top当前最顶层窗口.self表示当前打开的窗口 window.top.close(); 作用 ...
- 070_Shell 脚本对信号的处理,执行脚本后,按键盘 Ctrl+C 无法终止的脚本
#!/bin/bash#使用 trap 命令可以拦截用户通过键盘或 kill 命令发送过来的信号#使用 kill -l 可以查看 Linux 系统中所有的信号列表,其中 2 代表 Ctrl+C#tra ...
- Poj 2114 Boatherds(点分治)
Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...
- Echart-无需json文件的树状图(源码)超级简单,小白的福音
源码: <!DOCTYPE html> <head> <meta charset="utf-8"> <script type=" ...
- nodeJs 初学案例摘要
在学习nodeJs的时候,照着文档做的,但是到最后的上传文件显示图片总是报错, 所用的fs.renameSync出错:Error: EXDEV, cross-device link not permi ...
- python学习之模块:xlsxwriter
1.安装xlsxwriter模块 pip install xlsxwriter 2.使用 import xlsxwriter workbook = xlsxwriter.Workbook('hello ...
- WebSocketSharp中WebSocket类
websocket-sharp.clone, Version=1.0.2.39869 WebSocket由方法调用事件改为实例化委托调用,两种构造 1.构造函数 第一种 // // 摘要: // In ...
- qt 关于Qt中MVC的介绍与使用
Qt包含一组使用模型/视图结构的类,可以用来管理数据并呈现给用户.这种体系结构引入的分离使开发人员更灵活地定制项目,并且提供了一个标准模型的接口,以允许广泛范围的数据源被使用到到现有的视图中. 模型 ...
- Thrift: Scalable Cross-Language Services Implementation
http://thrift.apache.org/static/files/thrift-20070401.pdf
- MySQL 8.0: From SQL Tables to JSON Documents (and back again)
MySQL 8.0: From SQL Tables to JSON Documents (and back again) | MySQL Server Bloghttps://mysqlserver ...