service controller来安装、卸载、控制服务
2015-11-03 14:12
197 查看
首先将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; } }
相关文章推荐
- 使用POI读写word docx文件
- 项目代码matlab
- java通过模拟post方式提交表单实现图片上传功能实例
- 2015年10月21日--2015年11月10日(平均每天3小时,15天,共47小时,剩3350小时)
- ibatis的实现
- ibatis的实现
- 从”JAVA“而终 7:java运行过程以及各变量初始化顺序
- myeclipse修改内存大小不足
- Swift 入门基础(结构体/类)
- iOS 开发实践之 Auto Layout
- jar包介绍
- Permutation Sequence
- android linearlayout 的gravity属性
- centos7上搭建jenkins环境
- TestNG同时使用DataProvider和Parameters
- java中id为封装类时整形new出的对象为null
- Position
- 《C++ primer》英文第五版阅读笔记(九)——string(字符串)类型
- iOS CoreAnimation系列教程
- Daily Scrumming* 2015.11.3(Day 15)