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 第三步 ...
随机推荐
- css基础四
过渡属性 下面的表格列出了所有的转换属性: 属性 描述 CSS transition 简写属性,用于在一个属性中设置四个过渡属性. 3 transition-property 规定应用过渡的 CSS ...
- 阿里巴巴矢量库IconFont__使用小录
使用阿里巴巴矢量库方法虽然不难,但本人记性不好,遂在次记录几笔 阿里巴巴矢量库地址:http://www.iconfont.cn/plus 阿里巴巴矢量库图标好处: 1:图标矢量化 2:自己总结:ic ...
- 【sqli-labs】 less32 GET- Bypass custom filter adding slashes to dangrous chars (GET型转义了'/"字符的宽字节注入)
转义函数,针对以下字符,这样就无法闭合引号,导致无法注入 ' --> \' " --> \" \ --> \\ 但是,当MySQL的客户端字符集为gbk时,就可能 ...
- webpack command not found 的意外的坑 - 原因是从node开始
写给自己做个记录: 弄了半天 执行了下面操作 npm install webpack -g 因为小白不懂原理,所以执行了好遍,结果还是如题, webpack command not found 网上搜 ...
- 【CF1173D】NanuuAndCircle
题目链接:http://codeforces.com/contest/1173/problem/D 赛场上弱爆了的小菜鸡(本人),怎么也没想到这道看起来近似于神仙计数/生成函数的题正解竟然如此简洁. ...
- win安装配置Java8环境
这里就不重复造轮子,搜索一下. 一堆就出来 这里就引用一个百度知道的经验 https://jingyan.baidu.com/article/48b558e3f135687f38c09a03.html ...
- UOJ #277 BZOJ 4739 定向越野 (计算几何、最短路)
手动博客搬家: 本文发表于20181208 14:39:01, 原地址https://blog.csdn.net/suncongbo/article/details/84891710 哇它居然显示出图 ...
- Easyphp让其他电脑访问
1.将httpd.conf中的Listen 127.0.0.1:80,修改为Listen 80. 2.重启
- hdu 3594 强连通好题仙人掌图,对自己的tarjan模板改下用这个
#include<stdio.h> #include<string.h> #define N 21000 struct node { int v,next; }bian[510 ...
- 0419MySQL 中 Join 的基本实现原理
转自http://www.kuqin.com/database/20081206/29717.html 简朝阳 JOIN的用法你真的知道吗? 在 MySQL 中,只有一种 Join 算法,就是大名鼎鼎 ...