1. public ActionResult ListService()
  2. { //获取已经保存好的windows服务名称
  3. IList<Model.ReportServicesInfoEnt> List =GetServiceNameList;
  4. ServiceController serviceObject = null;
  5. foreach (Model.ReportServicesInfoEnt e in List)
  6. {
  7. FileInfo fi= GetWindowsServiceInstallPath(e.ServiceName);
  8. if (fi != null)
  9. {
  10. e.FilePath = fi.FullName;
  11. serviceObject = GetServiceObject(e.ServiceName);
  12. if (serviceObject != null)
  13. {
  14. e.StatusName = serviceObject.Status.ToString();
  15. }
  16. }
  17.  
  18. }
  19. ViewData["Count"] = Count;
  20. ViewData["ReportServicesInfoEnt"] = List;
  21. return View();
  22. }
  23.  
  24. #region 服务操作
  25. /// <summary>
  26. /// 服务操作
  27. /// </summary>
  28. /// <param name="serviceName"></param>
  29. /// <param name="Type">5表示启动,10表示重启,15表示停止</param>
  30. /// <returns></returns>
  31. public ActionResult SetService(string serviceName, int SetType)
  32. {
  33. var msg = "";
  34. switch (SetType)
  35. {
  36. case :
  37. msg = "启动";
  38. break;
  39. case :
  40. msg = "重启";
  41. break;
  42. case :
  43. msg = "停止";
  44. break;
  45. }
  46. var ret = new
  47. {
  48. Success = true,
  49. Message = msg + "完成"
  50. };
  51.  
  52. return Json(ret, JsonRequestBehavior.AllowGet);
  53. }
  54. /// <summary>
  55. /// 获取windows服务实例
  56. /// </summary>
  57. /// <param name="ServiceName"></param>
  58. /// <returns></returns>
  59. private ServiceController GetServiceObject(string ServiceName)
  60. {
  61. return new ServiceController(ServiceName);
  62. }
  63. /// <summary>
  64. /// 获取服务注册表信息
  65. /// </summary>
  66. /// <param name="ServiceName"></param>
  67. /// <returns></returns>
  68. private FileInfo GetWindowsServiceInstallPath(string ServiceName)
  69. {
  70. string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName;
  71. if (Registry.LocalMachine.OpenSubKey(key) == null)
  72. {
  73. return null;
  74. }
  75. else
  76. {
  77. string path = Registry.LocalMachine.OpenSubKey(key).GetValue("ImagePath").ToString();
  78. path = path.Replace("\"", string.Empty);
  79. FileInfo fi = new System.IO.FileInfo(path);
  80. return fi;
  81. }
  82. }
  83. /// <summary>
  84. /// 重启
  85. /// </summary>
  86. /// <param name="sc"></param>
  87. private void ReStarService(ServiceController sc)
  88. {
  89. this.StopService(sc);
  90. this.StarService(sc);
  91. }
  92. /// <summary>
  93. /// 停止服务
  94. /// </summary>
  95. /// <param name="sc"></param>
  96. private void StopService(ServiceController sc)
  97. {
  98. if ((sc.Status == ServiceControllerStatus.Paused) || (sc.Status == ServiceControllerStatus.Running))
  99. {
  100. sc.Stop();
  101. }
  102. }
  103. /// <summary>
  104. /// 开始服务
  105. /// </summary>
  106. /// <param name="sc"></param>
  107. private void StarService(ServiceController sc)
  108. {
  109. if ((sc.Status == ServiceControllerStatus.Paused) || (sc.Status == ServiceControllerStatus.Stopped))
  110. {
  111. sc.Start();
  112. }
  113. }
  114. #endregion

通过asp.net程序来控制自己开发的windows服务的更多相关文章

  1. C# 开发的windows服务 不能调试——讨论整理

    CSDN的标题:C# 开发的windows服务 不能调试 System.Diagnostics.Debugger.Launch();在想加断点的地方加入这行,是进入断点的,可以进行调试,我的是xp系统 ...

  2. 在ASP.NET Core中使用Apworks开发数据服务:对HAL的支持

    HAL,全称为Hypertext Application Language,它是一种简单的数据格式,它能以一种简单.统一的形式,在API中引入超链接特性,使得API的可发现性(discoverable ...

  3. .net开发windows服务

    最近一个月都异常的繁忙,项目进度非常的紧,回头看看自己的blog,整整一个5月都没有一篇文章,非常惭愧,现在补几篇文章,介绍一下我最近关注的技术.这篇文章将介绍Windows服务程序的开发.摘要:本文 ...

  4. C#开发可以可视化操作的windows服务

    使用C#开发自定义windows服务是一件十分简单的事.那么什么时候,我们需要自己开发windows服务呢,就是当我们需要计算机定期或者一 直执行我们开发的某些程序的时候.我经常看到许多人开发的win ...

  5. 【C#】开发可以可视化操作的windows服务

    使用C#开发自定义windows服务是一件十分简单的事.那么什么时候,我们需要自己开发windows服务呢,就是当我们需要计算机定期或者一直执行我们开发的某些程序的时候.这里我以一个WCF的监听服务为 ...

  6. C#开发人员能够可视化操作windows服务

    使用C#开发自己的定义windows服务是一个很简单的事.因此,当.我们需要发展自己windows它的服务.这是当我们需要有定期的计算机或运行某些程序的时候,我们开发.在这里,我有WCF监听案例,因为 ...

  7. Topshelf 一个简化Windows服务开发的宿主服务框架

    Topshelf是 基于.net框架开发的宿主服务框架.该框架简化了服务的创建,开发人员只需要使用 Topshelf编写一个控制台程序,就能安装为Windows服务.之所以这样原因非常简单:调试一个控 ...

  8. 利用Topshelf把.NET Core Generic Host管理的应用程序部署为Windows服务

    背景 2019第一篇文章. 此文源于前公司在迁移项目到.NET Core的过程中,希望使用Generic Host来管理定时任务程序时,没法部署到Windows服务的问题,而且官方也没给出解决方案,只 ...

  9. C#开发windows服务如何调试——资料整理

    原文标题:C# Windows服务程序如何进行调试 原文地址:https://jingyan.baidu.com/article/456c463b18e1b00a583144b3.html 第一种: ...

随机推荐

  1. Byte measurements

  2. hdu 5120 Intersection

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5120 A ring is a 2-D figure bounded by two circles sh ...

  3. 【POJ】【2449】Remmarguts' Date

    K短路/A* 经(luo)典(ti) K短路题目= = K短路学习:http://www.cnblogs.com/Hilda/p/3226692.html 流程: 先把所有边逆向,做一遍dijkstr ...

  4. Java compiler level does not match the version of the instal

    一.问题描述 新建了一个项目,workspace默认jdk编译版本是1.7的,新建项目使用的是jdk1.5的版本,肯定会报@override错误.这个时候,修改项目的compilor即可. 这时候,你 ...

  5. Leetcode#166 Fraction to Recurring Decimal

    原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...

  6. 数据导出为excel表格

    ---恢复内容开始--- 方式一: 通过request和response中携带的数据导出表格,导出的结果会将页面中展示的内容全部导出.代码如下: //调出保存框,下载页面所有内容 String fil ...

  7. mongo 1067错误

    对mongo进行错误的操作导致mongo服务异常关闭,当重启mongo服务时出现1067错误此时在data目录下产生mongod.lock文件,可以讲此文件删除,然后重启就可以了 Please mak ...

  8. Sqli-labs less 34

    Less-34 本关是post型的注入漏洞,同样的也是将post过来的内容进行了 ' \ 的处理.由上面的例子可以看到我们的方法就是将过滤函数添加的 \ 给吃掉.而get型的方式我们是以url形式提交 ...

  9. 使用命令行编译、打包、运行WordCount--不用eclipse

    1)首先创建WordCount1023文件夹,然后在此目录下使用编辑器,例如vim编写WordCount源文件,并保存为WordCount.java文件 /** * Licensed under th ...

  10. linux 上传/下载文件到windows工具

    一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用ssh登陆linux主机以后,如何能够快速的和本地机器进行文件的交互呢,也就是上传和下载文件到服务器和本地:   与ssh ...