zoukankan      html  css  js  c++  java
  • service controller来安装、卸载、控制服务

    首先将windows service发布到web的bin目录

        public class HomeController : Controller
        {
            string sName = "MyService1";
         
            public ActionResult Index()
            {
    
                
                try
                {
                    ServiceController sc = new ServiceController(sName);
                    ViewBag.Statu = sc.Status.ToString();
                }
                catch (Exception e)
                {
    
                    ViewBag.Statu = e.Message;
                }
                
                return View();
            }
    
            public ActionResult StartService()
            {
               
    
                try
                {
                    ServiceController sc = new ServiceController(sName);
                    sc.Refresh();
                    if (sc.Status == ServiceControllerStatus.Stopped)
                    {
                        sc.Start();
                        sc.WaitForStatus(ServiceControllerStatus.Running);
                        sc.Close();
                    }
                }
                catch (Exception e)
                {
                    ViewBag.Statu = e.Message;
                }
                
                return null;
            }
    
            public ActionResult StopService()
            {
                try
                {
                    ServiceController sc = new ServiceController(sName);
    
                    if (sc.Status == ServiceControllerStatus.Running)
                    {
                        sc.Stop();
                        sc.WaitForStatus(ServiceControllerStatus.Stopped);
                        sc.Close();
                    }
                }
                catch (Exception e)
                {
                    
                    throw e;
                }
               
                return null;
            }
    
            public ActionResult ReStartService()
            {
                ServiceController sc = new ServiceController(sName);
                if (sc != null)
                {
                    if (sc.Status == ServiceControllerStatus.Stopped)
                    {
                        sc.Start();
                        sc.WaitForStatus(ServiceControllerStatus.Running);
    
                    }
                    else if (sc.Status == ServiceControllerStatus.Running)
                    {
                        sc.Stop();
                        sc.WaitForStatus(ServiceControllerStatus.Stopped);
                        sc.Start();
                        sc.WaitForStatus(ServiceControllerStatus.Running);
    
                    }
                    sc.Close();
                }
              
                return null;
            }
            public ActionResult InstallService()
            {
                
                IDictionary stateSaver=new Hashtable();
                string[] cmdLine = { };
            
    
                ServiceController sc = new ServiceController(sName);
                if(!ServiceIsExisted(sName))
                {
                    try
                    {
                        AssemblyInstaller installer = new AssemblyInstaller();
                        installer.Path = Server.MapPath("/bin/WindowsServiceTest.exe");
    
                        installer.UseNewContext = true;
                        installer.Install(null);
                        installer.Commit(null);
                        installer.Dispose();
    
                        
                    }
                    catch (Exception e)
                    {
                        
                        throw e;
                    }
                
                }
                sc.Close();
                //WindowsServiceTest.vshost.exe
                return null;
            }
            public ActionResult UninstallService()
            {
               
                ServiceController sc = new ServiceController(sName);
                if (ServiceIsExisted(sName))
                {
                    try
                    {
                        
                     
                        AssemblyInstaller installer = new AssemblyInstaller();
                        installer.UseNewContext = true;
                        installer.Path = Server.MapPath("/bin/WindowsServiceTest.exe");
                        installer.Uninstall(null);
    
                    }
                    catch (Exception e)
                    {
    
                        throw e;
                    }
    
                }
                sc.Close();
                return null;
            }
    
            private bool ServiceIsExisted(string sName)
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController s in services)
                {
                    if (s.ServiceName == sName)
                    {
                        return true;
                    }
                }
                return false;
            }
    
        }
  • 相关阅读:
    Poj 1742 Coins(多重背包)
    Poj 2350 Above Average(精度控制)
    求二进制数中1的个数
    Poj 1659 Distance on Chessboard(国际象棋的走子规则)
    Poj 2411 Mondriaan's Dream(压缩矩阵DP)
    Poj 2136 Vertical Histogram(打印垂直直方图)
    Poj 1401 Factorial(计算N!尾数0的个数——质因数分解)
    poj 2390 Bank Interest(计算本利和)
    Poj 2533 Longest Ordered Subsequence(LIS)
    Poj 1887 Testing the CATCHER(LIS)
  • 原文地址:https://www.cnblogs.com/tgdjw/p/4933078.html
Copyright © 2011-2022 走看看