C#控制Window服务的状态
来源:互联网 发布:mac怎么查看下一张图片 编辑:程序博客网 时间:2024/06/07 18:44
最近想做这样一个效果,在网页控制Window服务启动和停止和状态:
添加引用
- Window服务在程序集System.ServiceProcess,然后增加using System.ServiceProcess;
- 服务有几个状态,它是枚举类型,如下:
// // 摘要: // 服务未运行。这对应于 Win32 SERVICE_STOPPED 常数,该常数定义为 0x00000001。 Stopped = 1, // // 摘要: // 服务正在启动。这对应于 Win32 SERVICE_START_PENDING 常数,该常数定义为 0x00000002。 StartPending = 2, // // 摘要: // 服务正在停止。这对应于 Win32 SERVICE_STOP_PENDING 常数,该常数定义为 0x00000003。 StopPending = 3, // // 摘要: // 服务正在运行。这对应于 Win32 SERVICE_RUNNING 常数,该常数定义为 0x00000004。 Running = 4, // // 摘要: // 服务即将继续。这对应于 Win32 SERVICE_CONTINUE_PENDING 常数,该常数定义为 0x00000005。 ContinuePending = 5, // // 摘要: // 服务即将暂停。这对应于 Win32 SERVICE_PAUSE_PENDING 常数,该常数定义为 0x00000006。 PausePending = 6, // // 摘要: // 服务已暂停。这对应于 Win32 SERVICE_PAUSED 常数,该常数定义为 0x00000007。 Paused = 7
更新服务类
- 代码如下
public static class WindowService { private const string SERVICE_NAME = "Fax"; //服务名称 /// <summary> /// 获取Window服务状态 /// </summary> /// <returns></returns> public static ServiceControllerStatus GetWinServiceState() { ServiceController sc = new ServiceController(SERVICE_NAME); return sc.Status; } /// <summary> /// 停止Window服务 /// </summary> public static void StopWinService() { ServiceController sc = new ServiceController(SERVICE_NAME); sc.Stop(); } /// <summary> /// 启动Window服务 /// </summary> public static void StartWinService() { ServiceController sc = new ServiceController(SERVICE_NAME); sc.Start(); } /// <summary> /// 刷新Window服务 /// </summary> public static void RestoreWinService() { ServiceController sc = new ServiceController(SERVICE_NAME); sc.Refresh(); } }
服务权限问题
- 部署可能会遇到权限问题,如下:
- 那么在可以修改应用程序池的标识,如下
- 以上为个人遇到问题,仅供参考!
阅读全文
0 0
- C#控制Window服务的状态
- C# 创建window服务
- C#创建Window服务
- C# 创建window服务
- c# 创建window 服务
- 利用java控制window服务
- 使用C#控制远程计算机的服务
- 使用C#控制远程计算机的服务
- 使用C#控制远程计算机的服务
- 使用C#控制远程计算机的服务
- C#控制远程计算机的服务
- C#控制远程计算机的服务
- 使用C#控制远程计算机的服务
- 使用C#控制远程计算机的服务
- 使用C#控制远程计算机的服务
- 使用C#控制远程计算机的服务
- 使用C#控制远程计算机的服务
- C#控制远程计算机的服务
- main函数一定要有返回值吗?
- linux rsync 使用说明
- Splunk 操作系统App和Add-on整理总结
- 习题3-10 盒子(Box, ACM/ICPC NEERC 2004, UVa1587)
- C++中友元的理解
- C#控制Window服务的状态
- "[TLIB Warning] Warning: library was too large for page size, rebuilt with page size 64"
- NSDateFormatter的dateStyle与timeStyle
- Linq中where查询
- 如何迁移git项目到gogs
- error Your local changes to the following files would be overwritten by merge
- java网络编程之Netty编解码技术(六)
- ubuntu 上更换python环境
- 以txt格式保存EditText的信息到SD中