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);
            }
        }
相關文章
相關標籤/搜索