基于C#实现Windows服务状态启动和停止服务的方法
2023-06-13 09:15:44 时间
本文以实例形式展示了基于C#实现Windows服务状态启动和停止服务的方法。非常实用。分享给大家供大家参考之用。具体方法如下:
首先先引用:
System.ServiceProcess.dll
然后在引用命名空间:
usingSystem.ServiceProcess;
建立服务对象:
ServiceControllersc=newServiceController("Server");
服务运行则停止服务:
if(sc.Status.Equals(ServiceControllerStatus.Running)) { sc.Stop(); sc.Refresh(); }
服务停止则启动服务:
if((sc.Status.Equals(ServiceControllerStatus.Stopped))||(sc.Status.Equals(ServiceControllerStatus.StopPending))) { sc.Start(); sc.Refresh(); }
希望本文所述对大家的C#程序设计有所帮助
相关文章
- C# 正则表达式大全「建议收藏」
- c#数组赋初值_C#数组初始化
- Luogu P3413 SAC#1 - 萌数 题解
- C#中Dock属性的作用[通俗易懂]
- C#中Lock的秘密
- windows从Mingw到Linux,再到Windows:一程多操作系统之旅(mingwlinux)
- windows从Quagga到Linux再到Windows:跨平台体验(quaggalinux)
- Windows登录MySQL数据库的步骤指南(windows登录mysql)
- 从Windows传输文件至Linux(windows传文件到linux)
- Linux系统下安装Windows软件的简便方法(linux安装windows软件)
- 前瞻:Windows远程访问Linux技术(windows远程访问linux)
- 在windows 中无效Oracle Preferences Invalid on Windows Platform(oracle首选项)
- 安装Linux虚拟机:在Windows系统中的新体验(windows安装linux虚拟机)
- 跨平台远程办公:Windows 远程桌面上的 Linux(windows远程桌面linux)
- 和windows简单实用:同时装双系统Linux和Windows(电脑装双系统linux)
- 双系统安装:Linux和Windows如何同时安装?(如何安装linux和windows双系统)
- 切换双系统:Linux与Windows的交互(linux和windows双系统)
- Linux轻松移植文件至Windows(从linux下载文件到windows)
- 比较:Windows 与 Linux 的异同(windows和linux)
- 对比分析:Linux系统与Windows系统的异同(linux系统与windows系统的区别)
- C#中string与byte[]的转换帮助类-.NET教程,C#语言
- C#列出局域网中可用SQLServer服务器
- C#加密在实际中的应用
- C#读取指定路径配置文件的方法
- 用C#控制Windows系统音量的实现方法
- 浅析C#中的Main(String[]args)参数输入问题
- c#使用多线程的几种方式示例详解
- c#创建windows服务入门教程实例
- C#面向对象编程之猜拳游戏实现方法
- C#WinForm窗体编程中处理数字的正确操作方法