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;
            }
    
        }
  • 相关阅读:
    PHP线程安全
    Oracle中MD5+Base64加密实现
    1002. A+B for Polynomials (25)
    1001. A+B Format (20)
    Rails,uva 514
    Database,Uva1592
    Hello World for U
    D3.js 力导向图
    从零开始CSS(一 2016/9/21)
    从零开始HTML(三 2016/9/20)
  • 原文地址:https://www.cnblogs.com/tgdjw/p/4933078.html
Copyright © 2011-2022 走看看