c# Windows服务管理
.NET Framework中提供的类库可以很方便的实现对windows服务的安装、卸载、启动、停止、获取运行状态等功能。这些类都在System.ServiceProcess命名空间下。
所以,在开始编写程序之前,需要先引用System.ServiceProcess。
获取Windows服务列表:
// 获取服务列表
ServiceController[] serviceList = ServiceController.GetServices();
// 按名称排序
serviceList = serviceList.OrderBy(m => m.DisplayName).ToArray();
// 遍历服务列表
foreach (ServiceController sc in serviceList)
{
// 服务信息
}
启动服务:
string serviceName="服务名称";
ServiceController sc = new ServiceController(serviceName); //建立服务对象
if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running); //等待启动
sc.Refresh();
}
停止服务:
string serverName="服务名称";
ServiceController sc = new ServiceController(serviceName); //建立服务对象
if (sc.Status.Equals(ServiceControllerStatus.Running))
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped); //等待停止
sc.Refresh();
}
重启服务:
string serviceName = "服务名称";
ServiceController sc = new ServiceController(serviceName); //建立服务对象
if (sc.Status.Equals(ServiceControllerStatus.Running))
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped); //等待停止
sc.Refresh();
}
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running); //等待启动
sc.Refresh();
c# Windows服务管理的更多相关文章
- windows 服务管理器使用系统内置帐户时密码的输入
windows 服务管理器使用系统内置帐户时在选择帐户如network services后不需要输入密码,直接确认即可,系统会自动附加密码.
- windows服务管理操作
服务程序是windows上重要的一类程序,它们虽然不与用户进行界面交互,但是它们对于系统有着重要的意义.windows上为了管理服务程序提供了一个特别的程序:服务控制管理程序,系统上关于服务控制管理的 ...
- Windows服务管理
按键:win+R 输入:services.msc “服务和应用程序”界面选项打开 * sc命令的使用:create(创建) delete(删除)等 * service可执行文件路径的修改:win+R ...
- windows服务管理TopShelf
http://docs.topshelf-project.com/en/latest/index.html 我曾经把名称错为Topself
- 玩转Windows服务系列——使用Boost.Application快速构建Windows服务
玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...
- RDIFramework.NET框架SOA解决方案(集Windows服务、WinForm形式与IIS形式发布)-分布式应用
RDIFramework.NET框架SOA解决方案(集Windows服务.WinForm形式与IIS形式发布)-分布式应用 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架 ...
- 如何将控制台程序包装成windows服务
1. 新建一个项目,或者从选择当前解决方案--右键-添加--新建项目 2. 选择(项目类型)Visual C#项目,(模板)Windows 服务,填写要创建的服务名称(修改默认的WindowServi ...
- .net Windows服务程序和安装程序制作图解 及 VS 2010创建、安装、调试 windows服务(windows service)
.net Windows服务程序和安装程序制作 最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作 ...
- 为C# Windows服务添加安装程序
最近一直在搞Windows服务,也有了不少经验,感觉权限方面确定比一般程序要受限很多,但方便性也很多.像后台运行不阻塞系统,不用用户登录之类.哈哈,扯远了,今天讲一下那个怎么给Windows服务做个安 ...
随机推荐
- zabbix3.4配置第三方邮件报警
废话不多说,直接进入主题. 1.安装mailx [root@localhost ~]#yum install malix -y #yum安装malix [ ...
- Nginx Rewrite相关功能-rewrite指令
Nginx Rewrite相关功能-rewrite指令 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- shell 脚本中 while 只执行一次
实例代码 while read line ; do ssh -p20002 $line -o StrictHostKeyChecking=no xxxxxxxxx done < ip.txt w ...
- 前端性能优化 http请求的过程及潜在的优化点
CS架构:比如我们的代码开发好,打包成apk,发布到平台,那么最终怎么运行到用户的手机上呢,用户首先需要从相关的应用商城下载这个apk包,并且运行这个 apk 包,那么这个 apk 包就会被解压,最后 ...
- ubuntu无法连接网络
一,先说一下VMware软件和linux镜像的版本: VMware Workstation 14 Pro ubuntu-14.04.6-desktop-amd64.iso 二,遇到的问题 在第一次安装 ...
- maven 配置文件
<properties> <project.builder.sourcesEncoding>UTF-8</project.builder.sourcesEncoding& ...
- bootstrap ui样例
http://demo.codedefault.com/demo/ui/theadmin/samples/invoicer/settings.html
- VIJOS-P1446 最短路上的统计
JDOJ 1523: VIJOS-P1446 最短路上的统计 JDOJ传送门 Description 一个无向图上,没有自环,所有边的权值均为1,对于一个点对(a,b),我们要把所有a与b之间所有最短 ...
- NOIP 2013 火柴排队
洛谷 P1966 火柴排队 洛谷传送门 JDOJ 2227: [NOIP2013]火柴排队 D1 T2 JDOJ传送门 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高 ...
- 修改/etc/docker/daemon.json中的log-opts配置发现无效 docker 限制日志大小
https://colobu.com/2018/10/22/no-space-left-on-device-for-docker/ 在/etc/docker/daemon.json中修改或添加log- ...