Web启动,停止Windows服务
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);
}
}
}
}
}
运行结果如图:
Web启动,停止Windows服务的更多相关文章
- delphi 启动停止windows服务 转
http://blog.csdn.net/haiou327/article/details/6106233 不用cmd用delphi如何实现启动停止windows服务建议参考一下Delphi的Sckt ...
- Delphi启动/停止Windows服务,启动类型修改为"自动"
unit U_StartServices; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Contr ...
- Linux Systemd——在RHEL/CentOS 7中启动/停止/重启服务
RHEL/CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理.systemd兼容SysV和Li ...
- bat启动/停止oracle服务
原文:bat启动/停止oracle服务 自己的电脑比较慢,尤其装了oracle10g后,服务开启和关闭用bat文件操作省事点 开启服务 @echo offnet start OracleService ...
- 在CentOS 7中启动/停止/重启服务
RHEL/CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理.systemd兼容SysV和Li ...
- 注册、启动、停止windows服务
找到本机InstallUtil.exe命令 命令行下注册服务InstallUtil.exe D:\XXXXService.exe 启动服务 net start XXXXService 停止服务net ...
- Windows下使用service.bat安装tomcat服务, 启动停止tomcat服务
在项目开发过程中,以前只是在Eclipse中配置.启动.停止tomcat服务器 如果只想在机器中使用tomcat服务器,而不想安装MyEclipse,可以使用service.bat 将tomcat安装 ...
- web站点和windows服务项目发布时如何排除指定文件
在发布asp.net站点和windows服务项目时,有的时候这样的需求:msbuild编译之后发布到服务器指定目录时要排除指定文件,比如通过jenkins构建时,不希望覆盖原来的Web.config和 ...
- 【OF框架】使用OF.WinService项目,添加定时服务,进行创建启动停止删除服务操作
准备 使用框架搭建完成项目,包含OF.WinService项目. 了解Window Service 和定时服务相关知识. 一.添加一个定时服务 第一步:了解项目结构 第二步:创建一个新的Job 第三步 ...
随机推荐
- 读书笔记「Python编程:从入门到实践」_7.用户输入和while循环
7.1 函数input()的工作原理 函数input() 让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. message = input(&qu ...
- 集合运算(UNION)
表的加法 集合运算:就是满足统一规则的记录进行的加减等四则运算. 通过集合运算可以得到两张表中记录的集合或者公共记录的集合,又或者其中某张表中记录的集合. 集合运算符:用来进行集合的运算符. UNIO ...
- printf 打印较长字符
- PAT_A1147#Heaps
Source: PAT A1147 Heaps (30 分) Description: In computer science, a heap is a specialized tree-based ...
- php第六讲
继承和静态 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- grpc-web与react的集成
很久没写总结了,在这里跟大家分享一下自己踩的坑,同时也方便自己多记忆下. 大致流程: 使用create-react-app脚手架生成react相关部分,脚手架内部会通过node自动起一个客户端,然后和 ...
- DOM学习之图片库切换效果
addloadevent(prepareplaceholder()) addloadevent(prepareGallery()) //页面加载完时执行函数 function addloadevent ...
- PAT 1065. A+B and C
Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C. Input S ...
- 运行npm run watch时报:events.js:182 throw er; // Unhandled 'error' event
I had this issue i did the following steps and i have no issues anymore: Delete node_modules directo ...
- poj 3177&&poj 3352加边构双联通(有重边)用tarjan 模板求的
#include<stdio.h>/* 求边双联通分量和求强连通差不多,先缩点求出叶子节点的个数 */ #include<string.h> #define N 5100 st ...