如何实现C#启动Windows服务及关闭
发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,如何实现C#启动Windows服务及关闭,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。C#启动Windows服务及关闭实例实现的步骤和使
千家信息网最后更新 2025年02月04日如何实现C#启动Windows服务及关闭
如何实现C#启动Windows服务及关闭,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
C#启动Windows服务及关闭实例实现的步骤和使用调用的类是什么呢?让我们来看看具体的实现:
C# 控制Windows服务的启动与关闭
// ...... using System.ServiceProcess; // ...... ////// Restart windows service /// /// ///the windows service display name ///If the restart successfully ///return true else return false public static bool RestartWindowsService(string serviceName) { bool bResult = false; try { try { //C#启动Windows服务及关闭 StopWindowsService(serviceName); Thread.Sleep(1000); } catch (Exception ex) { StartWindowsService(serviceName); Thread.Sleep(1000); StopWindowsService(serviceName); Thread.Sleep(1000); Console.WriteLine(ex.Message); } try { StartWindowsService(serviceName); Thread.Sleep(1000); } catch (Exception ex) //C#启动Windows服务及关闭{ StopWindowsService(serviceName); Thread.Sleep(1000); StartWindowsService(serviceName); Thread.Sleep(1000); Console.WriteLine(ex.Message); } bResult = true; } catch (Exception ex) { bResult = false; throw ex; } return bResult; } //C#启动Windows服务及关闭////// Start windows service /// /// ///the windows service display name ///If the start successfully ///return true else return false public static bool StopWindowsService(string serviceName) { ServiceController[] scs = ServiceController.GetServices(); bool bResult = false; foreach (ServiceController sc in scs) { if (sc.DisplayName == serviceName) { try { sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30)); sc.Stop(); bResult = true; } catch (Exception ex) { bResult = false; throw ex; } } } return bResult; } //C#启动Windows服务及关闭////// Stop windows service /// /// the windows ///service display name ///If the stop successfully ///return true else return false public static bool StartWindowsService( string serviceName) { ServiceController[] scs = ServiceController.GetServices(); bool bResult = false; foreach (ServiceController sc in scs) { if (sc.DisplayName == serviceName) { try { sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30)); sc.Start(); bResult = true; } catch (Exception ex) { bResult = false; throw ex; } } //C#启动Windows服务及关闭 } return bResult; }
C#启动Windows服务及关闭具体实现就向你介绍到这里,希望对你了解和学习C#启动Windows服务及关闭有所帮助。
看完上述内容,你们掌握如何实现C#启动Windows服务及关闭的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
服务
C#
内容
方法
更多
问题
束手无策
为此
原因
实例
对此
技能
步骤
篇文章
经验
行业
资讯
资讯频道
频道
学习
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
儿童网络安全
高速网络技术阶段
网络安全管理方案书籍
广州唐德互联网科技
sql 修改第一行数据库
后台管理网站服务器
软件开发需要学编程
株洲it软件开发培训课
龙岗区数据网络技术开发工艺
语音对讲软件开发哪些好办
徐汇区专业软件开发怎么样
局域网服务器能连接光纤吗
米游社怎么改服务器
bvd数据库公司
山西中小学网络安全教育
数据库的user表设计
中国互联网与科技公司
深圳电信服务器租用
2021年网络安全开幕式
用命令行查询数据库
当前网络安全的现状
万网电子病历清除数据库记录
扁桃我的世界行尸走肉服务器地址
上饶市网络安全建设招标
中国著名的软件开发商
深圳酒店软件开发公司
聊天软件开发到底哪家好
数据库可视化开源框架整合
服务器机柜电源线扎线
怎样维护计算机网络安全