using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;

namespace ScmWrapper
{
    public class ServiceHandler
    {
        #region 安装服务

/// <summary> 
        /// 安装服务 
        /// </summary> 
        public static bool InstallService(string nameService, string serviceFileName)
        {
            bool flag = true;
            if (!IsServiceIsExisted(nameService))
            {
                try
                {
                    using (Process myPro = new Process())
                    {
                        myPro.StartInfo.FileName = "cmd.exe";
                        myPro.StartInfo.UseShellExecute = false;
                        myPro.StartInfo.RedirectStandardInput = true;
                        myPro.StartInfo.RedirectStandardOutput = true;
                        myPro.StartInfo.RedirectStandardError = true;
                        myPro.StartInfo.CreateNoWindow = true;
                        myPro.Start();
                        //如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
                        string str = $"{serviceFileName} install &exit";

myPro.StandardInput.WriteLine(str);
                        myPro.StandardInput.AutoFlush = true;
                        myPro.WaitForExit();

}

}
                catch
                {
                    flag = false;
                }

//try
                //{
                //    InstallmyService(null, serviceFileName);
                //}
                //catch (Exception ex)
                //{
                //    flag = false;
                //}

}
            return flag;
        }
        #endregion

#region 卸载服务 
        /// <summary> 
        /// 卸载服务 
        /// </summary> 
        public static bool UninstallService(string nameService, string serviceFileName)
        {
            bool flag = true;
            if (IsServiceIsExisted(nameService))
            {
                if (IsServiceStart(nameService))
                {
                    StopService(nameService);
                }
                try
                {
                    using (Process myPro = new Process())
                    {
                        myPro.StartInfo.FileName = "cmd.exe";
                        myPro.StartInfo.UseShellExecute = false;
                        myPro.StartInfo.RedirectStandardInput = true;
                        myPro.StartInfo.RedirectStandardOutput = true;
                        myPro.StartInfo.RedirectStandardError = true;
                        myPro.StartInfo.CreateNoWindow = true;
                        myPro.Start();
                        //如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
                        string str = $"{serviceFileName} uninstall &exit";

myPro.StandardInput.WriteLine(str);
                        myPro.StandardInput.AutoFlush = true;
                        myPro.WaitForExit();

}
                }
                catch
                {
                    flag = false;
                }
                //try
                //{
                //    UnInstallmyService(serviceFileName);
                //}
                //catch
                //{
                //    flag = false;
                //}
            }
            return flag;
        }
        #endregion

#region 检查服务存在的存在性 
        /// <summary> 
        /// 检查服务存在的存在性 
        /// </summary> 
        /// <param name=" NameService ">服务名</param> 
        /// <returns>存在返回 true,否则返回 false;</returns> 
        public static bool IsServiceIsExisted(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            bool exist = services.Where(n => n.ServiceName.Equals(serviceName, StringComparison.OrdinalIgnoreCase))?.Count() > 0;
            services = null;
            return exist;
        }

#endregion

#region 判断window服务是否启动 
        /// <summary> 
        /// 判断某个Windows服务是否启动 
        /// </summary> 
        /// <returns></returns> 
        public static bool IsServiceStart(string serviceName)
        {
            ServiceController psc = new ServiceController(serviceName);
            bool bStartStatus = false;
            try
            {
                if (!psc.Status.Equals(ServiceControllerStatus.Stopped))
                {
                    bStartStatus = true;
                }

return bStartStatus;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                psc.Close();
                psc.Dispose();
            }
        }
        #endregion

#region  修改服务的启动项 
       
        public static void SetRecoveryOptions(string serviceName)
        {
            try
            {
                using (Process myPro = new Process())
                {
                    myPro.StartInfo.FileName = "cmd.exe";
                    myPro.StartInfo.UseShellExecute = false;
                    myPro.StartInfo.RedirectStandardInput = true;
                    myPro.StartInfo.RedirectStandardOutput = true;
                    myPro.StartInfo.RedirectStandardError = true;
                    myPro.StartInfo.CreateNoWindow = true;
                    myPro.Start();
                    //如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
                    string str = $"sc failure GeoFence reset=0 actions=restart/60000/restart/60000/restart/60000 &exit";

myPro.StandardInput.WriteLine(str);
                    myPro.StandardInput.AutoFlush = true;
                    myPro.WaitForExit();

}

}
            catch
            {
               
            }
        }

#endregion

#region 启动服务

public static bool StartService(string serviceName)
        {
            bool flag = true;
            if (IsServiceIsExisted(serviceName))
            {
                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                {
                    service.Start();
                    for (int i = 0; i < 60; i++)
                    {
                        service.Refresh();
                        System.Threading.Thread.Sleep(1000);
                        if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                        {
                            break;
                        }
                        if (i == 59)
                        {
                            flag = false;
                        }
                    }
                }
                service.Close();
                service.Dispose();
            }
            return flag;
        }
        #endregion

#region 停止服务

public static bool StopService(string serviceName)
        {
            bool flag = true;
            if (IsServiceIsExisted(serviceName))
            {
                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    service.Stop();
                    for (int i = 0; i < 60; i++)
                    {
                        service.Refresh();
                        System.Threading.Thread.Sleep(1000);
                        if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                        {
                            break;
                        }
                        if (i == 59)
                        {
                            flag = false;
                        }
                    }
                }
                service.Close();
                service.Dispose();
            }
            return flag;
        }
        #endregion

}

}

最新文章

  1. [NHibernate]持久化类(Persistent Classes)
  2. LinearRegressionWithRegularization
  3. CSS布局中——导航是非常常见的
  4. 最完美解决Nginx部署ThinkPHP项目的办法
  5. windows上SVN服务器以及客户端TortoiseSVN的安装配置
  6. /dev/urandom
  7. Highcharts教程2
  8. SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
  9. SVN 的revert操作
  10. java web 之 listen 与 filter
  11. 优化之XML组件
  12. Python从菜鸟到高手(2):清空Python控制台
  13. MT【67】窥一斑知全豹
  14. BZOJ.2034.[2009国家集训队]最大收益(二分图匹配 贪心)
  15. 上传文件到aws的s3存储
  16. Codeforces 652F Ants on a Circle
  17. 在没有创建Provision Profile权限的情况下 发布Enterprise inhouse app 的方法
  18. hashMap put方法 第二行代码
  19. [FJOI2015]火星商店问题
  20. Java线程池(一):初识

热门文章

  1. fiddler基本功能介绍
  2. antd中按需加载使用react-app-rewired报错
  3. 20172328 2018-2019《Java软件结构与数据结构》第八周学习总结
  4. 2019-2-14sql server数据库模糊查询语句
  5. dagger2 依赖注入
  6. 【安全性测试】一个简单地绕前端暴XSS漏洞
  7. android发送短信验证码并自动获取验证码填充文本框
  8. 织梦,dede:list和dede:artlist的区别
  9. 测试自动化学习3-python3简单操作
  10. ASO的效果应该如何去评判,有什么标准可以量化指标