Windows服务操作帮助类
/// <summary> /// 打开系统服务 /// </summary> /// <param name="serviceName">系统服务名称</param> /// <returns></returns> public static Tuple<bool, string> Open(string serviceName) { try { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status != ServiceControllerStatus.Running) { control.Start(); } } return new Tuple<bool, string>(true, "成功"); } catch (Exception e) { return new Tuple<bool, string>(false, e.Message); } } /// <summary> /// 关闭系统服务 /// </summary> /// <param name="serviceName">系统服务名称</param> /// <returns></returns> public static Tuple<bool, string> Close(string serviceName) { try { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Running) { control.Stop(); } } return new Tuple<bool, string>(true, "成功"); } catch (Exception e) { return new Tuple<bool, string>(false, e.Message); } } /// <summary> /// 重启系统服务 /// </summary> /// <param name="serviceName">系统服务名称</param> /// <returns></returns> public static Tuple<bool, string> ReStart(string serviceName) { try { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Running) { control.Continue(); } } return new Tuple<bool, string>(true, "成功"); } catch (Exception e) { return new Tuple<bool, string>(false, e.Message); } } /// <summary> /// 返回服务状态 /// </summary> /// <param name="serviceName">系统服务名称</param> /// <returns>1:服务未运行 2:服务正在启动 3:服务正在停止 4:服务正在运行 5:服务即将继续 6:服务即将暂停 7:服务已暂停 0:未知状态</returns> public static int GetSystemServiceStatus(string serviceName) { try { using (ServiceController control = new ServiceController(serviceName)) { return (int)control.Status; } } catch { return 0; } } /// <summary> /// 返回服务状态 /// </summary> /// <param name="serviceName">系统服务名称</param> /// <returns>1:服务未运行 2:服务正在启动 3:服务正在停止 4:服务正在运行 5:服务即将继续 6:服务即将暂停 7:服务已暂停 0:未知状态</returns> public static string GetSystemServiceStatusString(string serviceName) { try { using (ServiceController control = new ServiceController(serviceName)) { var status = string.Empty; switch ((int)control.Status) { case 1: status = "服务未运行"; break; case 2: status = "服务正在启动"; break; case 3: status = "服务正在停止"; break; case 4: status = "服务正在运行"; break; case 5: status = "服务即将继续"; break; case 6: status = "服务即将暂停"; break; case 7: status = "服务已暂停"; break; case 0: status = "未知状态"; break; } return status; } } catch { return "未知状态"; } } /// <summary> /// 安装服务 /// </summary> /// <param name="stateSaver"></param> /// <param name="filepath"></param> public static void Install(IDictionary stateSaver, string filepath) { try { AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller { UseNewContext = true, Path = filepath }; myAssemblyInstaller.Install(stateSaver); myAssemblyInstaller.Commit(stateSaver); myAssemblyInstaller.Dispose(); } catch (Exception ex) { throw new Exception("installServiceError/n" + ex.Message); } } /// <summary> /// 判断服务是否存在 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> public static bool Existed(string serviceName) { ServiceController[] services = ServiceController.GetServices(); return services.Any(s => s.ServiceName == serviceName); } /// <summary> /// 卸载服务 /// </summary> /// <param name="filepath">路径和文件名</param> public static void UnInstall(string filepath) { try { AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller { UseNewContext = true, Path = filepath }; myAssemblyInstaller.Uninstall(null); myAssemblyInstaller.Dispose(); } catch (Exception ex) { throw new Exception("unInstallServiceError/n" + ex.Message); } }
作 者:大師兄丶
出 处:http://www.cnblogs.com/zhao-yi
Git 地 址:https://github.com/ZhaoYis
个人博客:http://www.zhaoyis.com.cn
关于作者:主要从事基于.Net Framework平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!
出 处:http://www.cnblogs.com/zhao-yi
Git 地 址:https://github.com/ZhaoYis
个人博客:http://www.zhaoyis.com.cn
关于作者:主要从事基于.Net Framework平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!