Web启动,停止Windows服务
来源:互联网 发布:有了淘宝为啥还有天猫 编辑:程序博客网 时间:2024/05/16 12:17
When you grow stronger,the world become more dangerous.当你变得越强大,这个世界反而会变得越危险。
ServiceModel.cs代码:
public class ServiceModel { public string ServiceName { get; set; } public string DisplayName { get; set; } public bool IsRunning { get; set; } }
wServiceHandler.ashx代码:
using System;using System.Collections.Generic;using System.Linq;using System.ServiceProcess;using System.Web;namespace wServiceManager{ /// <summary> /// wServiceHandler 的摘要说明 /// </summary> public class wServiceHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //服务名 string serviceName = context.Request["serviceName"]; //操作类型【重启、停止、重启】 string type = context.Request["type"]; try { switch (type) { case "start": StartService(serviceName); break; case "stop": StopService(serviceName); break; case "reset": ResetService(serviceName); break; default: ResetService(serviceName); break; } context.Response.Write("ok"); } catch (Exception ex) { context.Response.Write(ex.Message); } } /// <summary> /// 启动服务 /// </summary> /// <param name="serviceName">服务名</param> private void StartService(string serviceName) { ServiceController service = new ServiceController(serviceName); if (service.Status == ServiceControllerStatus.Stopped) { service.Start(); service.WaitForStatus(ServiceControllerStatus.Running); service.Close(); } } /// <summary> /// 停止服务 /// </summary> /// <param name="serviceName">服务名</param> private void StopService(string serviceName) { ServiceController service = new ServiceController(serviceName); if (service.Status == ServiceControllerStatus.Running) { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); service.Close(); } } /// <summary> /// 重启服务 /// </summary> /// <param name="serviceName">服务名</param> private void ResetService(string serviceName) { ServiceController service = new ServiceController(serviceName); if (service.Status == ServiceControllerStatus.Running || service.Status == ServiceControllerStatus.Stopped) { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running); service.Close(); } } public bool IsReusable { get { return false; } } }}
IndexManager.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="IndexManager.aspx.cs" Inherits="wServiceManager.IndexManager" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="css/bootstrap.css" rel="stylesheet" type="text/css" /> <link href="css/bootstrap-theme.css" rel="stylesheet" type="text/css" /> <script src="js/jquery1.12.4.js" type="text/javascript"></script> <script src="js/bootstrap.js" type="text/javascript"></script> </head> <body> <form id="form1" runat="server"> <div class="container-fluid"> <div class="row-fluid"> <h3> Windows服务管理 </h3> <table class="table"> <thead> <tr> <th> 服务标识的名称 </th> <th> 服务的友好名称 </th> <th> 状态 </th> <th> 操作 </th> </tr> </thead> <tbody> <% int count = list.Count; for (int i = 0; i < count; i++) { string dname = list[i].DisplayName.Trim(); string sname = list[i].ServiceName.Trim(); string isRun = list[i].IsRunning ? "运行中" : "停止中"; %> <tr> <td> <%= dname %> </td> <td id="sname"> <%= sname %> </td> <td> <%= isRun %> </td> <td> <% if (list[i].IsRunning) { %> <button class="btn btn-danger" id="stopService" type="button"> 停止</button> <% } else { %> <button class="btn btn-success" id="startService" type="button"> 启动</button> <% } %> </td> </tr> <% } %> </tbody> </table> </div> </div> </form> <script type="text/javascript"> var sname = $("#sname").text().trim(); $("#startService").click(function() { $.ajax({ type: "post", url: "wServiceHandler.ashx", data: { "serviceName": sname, "type": "start" }, success: function(result) { if (result == "ok") { window.location.reload(); } } }); }); $("#stopService").click(function() { $.ajax({ type: "post", url: "wServiceHandler.ashx", data: { "serviceName": sname, "type": "stop" }, success: function(result) { if (result == "ok") { window.location.reload(); } } }); }); </script> </body></html>
IndexManager.aspx.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.ServiceProcess;namespace wServiceManager{ public partial class IndexManager : System.Web.UI.Page { public List<ServiceModel> list=new List<ServiceModel>(); protected void Page_Load(object sender, EventArgs e) { ServiceController[] myServices = ServiceController.GetServices(); list = new List<ServiceModel>(); foreach (var item in myServices) { if (item.ServiceType == ServiceType.Win32OwnProcess && item.DisplayName.Contains("memcached")) { ServiceModel model = new ServiceModel(); model.ServiceName = item.ServiceName; model.DisplayName = item.DisplayName; if(item.Status == ServiceControllerStatus.Running) { model.IsRunning = true; } else { model.IsRunning = false; } list.Add(model); } } } }}
运行结果如图:
0 0
- Web启动,停止Windows服务
- windows启动停止服务
- delphi启动停止windows服务
- C#启动停止windows服务
- C#启动,停止Windows服务
- 命令行方式 启动和停止 windows服务
- windows命令:启动停止某个服务
- Windows下启动停止Oracle11g服务
- Windows命令启动停止Oracle服务
- Windows服务之启动、停止、暂停、继续
- Windows下启动停止Oracle11g服务
- VC启动和停止windows系统服务
- 命令行创建,删除,启动,停止Windows服务
- .net 启动和停止Windows服务
- Windows下启动停止Oracle11g服务
- Windows下启动停止Oracle11g服务
- windows下快速启动和停止服务
- 启动、停止Windows服务的DOS命令
- ES6
- 通过PL/SQL Developer导出数据库表不好用的解决方案之一:
- 整数无序数组求第K大数(暴力|快排)
- iOS 浅谈:深.浅拷贝与copy.strong
- (十三)RabbitMQ消息队列-VirtualHost与权限管理
- Web启动,停止Windows服务
- Shadow Brokers公布黑客工具,您准备好防御了吗?
- docker学习笔记01:Centos安装docker
- SHOI 2008 仙人掌图 BZOJ 1023
- android6.0 动态权限申请
- 近期状态
- 日志学习:SLF4J & Log4J2
- http 消息头与 响应消息头 详解
- 忘掉Domino的认证吧,打造自己的认证系统