windows服务的停止和开启
2011-07-15 09:57
246 查看
/// <summary> /// WindowsServiceManage /// </summary> public class WindowsServiceManage { /// <summary> /// 重启服务 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> public static bool RestartWindowsService(string serviceName) { bool bResult = false; try { try { 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; } /// <summary> /// 停止服务 /// </summary> /// <param name="serviceName">服务名称</param> /// <returns></returns> 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; } /// <summary> /// 启动服务 /// </summary> /// <param name="serviceName">服务名称</param> /// <returns></returns> 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; } } } return bResult; } }
相关文章推荐
- ubuntu启动时开启或停止服务
- Windows 下ZooKeeper开启服务闪退的解决方法
- windows启动、停止和重新启动Apache服务
- Windows服务安装后自动停止
- windows 下memcached服务的启动与停止
- C# ASP.NET 控制Windows服务的开启、关闭、重启相关参考资料
- C#Windows服务:一些方法(启动、停止等)
- 安装 启动 停止 卸载 Windows服务 c#
- 本地计算机上的XXX服务启动后又停止了——Windows Service服务调试(二)
- Windows 服务程序创建与停止
- Windows开启SNMP服务----Win7
- 服务停止开启
- windows命令:启动停止某个服务
- rpcbind失败的可能原因:停止NetworkManager服务,等注册成功后再开启
- windows服务,安装、启动、停止,配置,一个批处理文件搞定
- C#制作Windows service服务系列一:制作一个可安装、可启动、可停止、可卸载的Windows
- Windows下的SNMP服务的开启
- C# 操作注册安装、开启、停止、卸载服务
- 统一开启域客户端”windows time”服务
- VMware不能连网解决方法——必须开启Windows的VMware相关服务