C# iis应用程序池控制:回收、启动、停止

https://blog.csdn.net/tanglingbo/article/details/98735819

C# iis应用程序池控制:回收、启动、停止
C# winfrom 写的程序

界面图:

实现功能:查询 iis应用程序池,回收、启动、停止

用到的dll:Microsoft.Web.Administration

dll路径:C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.9\bin\plugins\Diagnostics\Microsoft.Web.Administration.dll

界面代码:
————————————————

using Microsoft.Web.Administration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 测试IIS应用程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

/// <summary>
/// 记住IP
/// </summary>
string strIP="";

/// <summary>
/// 加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
button4_Click(null, null);
}

/// <summary>
/// 加载
/// </summary>
/// <param name="ip"></param>
List<iisITme> loadIISList(string ip)
{
List<iisITme> list = new List<iisITme>();
if (string.IsNullOrEmpty(ip))
{
MessageBox.Show("请输入本机IP地址!");
return list;
}
try
{
ServerManager serverManager = ServerManager.OpenRemote(ip);
if (serverManager == null)
{
throw new Exception("获取到数据为空!");
}
ApplicationPoolCollection appPools = serverManager.ApplicationPools;
if (appPools == null)
{
throw new Exception("获取到数据为空!");
}
if (appPools.Count <= 0)
{
throw new Exception("iis应用池 无数据!");
}
foreach (ApplicationPool ap in appPools)
{
string Name = ap.Name;
ObjectState state = ap.State;
iisITme myiisITme = new iisITme();
myiisITme.name = Name;
myiisITme.state = state;
list.Add(myiisITme);
}
list = list.OrderBy(m => m.name).ToList();
}
catch (Exception ex)
{
string str = ex.Message;
string show = "";
if (str.Contains("权限"))
{
show = "获取IIS应用池发送异常,IP:" + ip + ",描述:" + str + "\n 请用管理员身份运行!";
}
else
{
show = "获取IIS应用池发送异常,IP:" + ip + ",描述:" + str;
}
MessageBox.Show(show);

}
strIP = ip;
return list;
}

/// <summary>
/// 获取选择列表
/// </summary>
/// <returns></returns>
List<iisITme> GetCheckedList()
{
List<iisITme> list = new List<iisITme>();
int count = flpList.Controls.Count;
if (count <= 0)
{
MessageBox.Show("请加载应用池列表!");
return list;
}

foreach (var item in flpList.Controls)
{
CheckBox cb = item as CheckBox;
if (cb != null && cb.Checked)
{
iisITme myiisITme = new iisITme();
myiisITme.name = cb.Text;
myiisITme.state = (ObjectState)cb.Tag;
list.Add(myiisITme);
}
}
if (list == null || list.Count <= 0)
{
MessageBox.Show("请加选择 应用池!");
return list;
}

return list;
}

/// <summary>
/// 回收
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
List<iisITme> list = GetCheckedList();
if (list != null && list.Count > 0)
{
try
{
ServerManager serverManager = ServerManager.OpenRemote(strIP);
if (serverManager == null)
{
throw new Exception("获取到数据为空!");
}
ApplicationPoolCollection appPools = serverManager.ApplicationPools;
if (appPools == null)
{
throw new Exception("获取到数据为空!");
}
if (appPools.Count <= 0)
{
throw new Exception("iis应用池 无数据!");
}
int count = 0;
foreach (var item in list)
{
count = 0;
foreach (var ap in appPools)
{
string Name = ap.Name;
if (item.name == Name)
{
ObjectState ob = ap.Recycle();
count++;
break;
}

}
if (count == 0)
{
throw new Exception("应用池:" + item + " 不存在!");
}
}
MessageBox.Show("回收完成!");
button4_Click(null, null);
}
catch (Exception ex)
{
string str = ex.Message;
string show = "";
if (str.Contains("权限"))
{
show="IIS应用池异常,描述:" + str+"\n 请用管理员身份运行!";
}
else
{
show = "IIS应用池异常,描述:" + str;
}
MessageBox.Show(show);
button4_Click(null, null);
}

}
}

/// <summary>
/// 加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
flpList.Controls.Clear();
string ip =txtIP.Text.Trim();
List<iisITme> list= loadIISList(ip);
if (list != null && list.Count>0)
{

}
foreach (var item in list)
{
CheckBox cb=new CheckBox ();
cb.Text=item.name;
Color color=Color.Black;
if (item.state == ObjectState.Starting)//0 该对象正在启动过程中。
{
color=Color.Green;
}
else if (item.state == ObjectState.Started)//1 该网站,ApplicationPool,或WorkerProcess对象已经开始。
{
color=Color.Green;
}
else if (item.state == ObjectState.Stopping)//2 该物体正在停止。
{
color=Color.Gray;
}
else if (item.state == ObjectState.Stopped)//3 该对象已停止。
{
color=Color.Gray;
}
else if (item.state == ObjectState.Unknown)//4 对象处于未知状态。
{
color=Color.Red;
}
cb.ForeColor = color;
cb.Tag = item.state;
Font f = new Font (cb.Font.FontFamily, 12);
cb.Font =f;
flpList.Controls.Add(cb);
}
}

/// <summary>
/// 启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
List<iisITme> list = GetCheckedList();
if (list != null && list.Count > 0)
{
try
{
ServerManager serverManager = ServerManager.OpenRemote(strIP);
if (serverManager == null)
{
throw new Exception("获取到数据为空!");
}
ApplicationPoolCollection appPools = serverManager.ApplicationPools;
if (appPools == null)
{
throw new Exception("获取到数据为空!");
}
if (appPools.Count <= 0)
{
throw new Exception("iis应用池 无数据!");
}
int count = 0;
foreach (var item in list)
{
count = 0;
foreach (var ap in appPools)
{
string Name = ap.Name;
if (item.name == Name)
{
ObjectState ob = ap.Start();
count++;
break;
}

}
if (count == 0)
{
throw new Exception("应用池:" + item + " 不存在!");
}
}
MessageBox.Show("启动完成!");
button4_Click(null, null);
}
catch (Exception ex)
{
MessageBox.Show("IIS应用池异常,描述:" + ex.Message);
button4_Click(null, null);
}

}
}

/// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
List<iisITme> list = GetCheckedList();
if (list != null && list.Count > 0)
{
try
{
ServerManager serverManager = ServerManager.OpenRemote(strIP);
if (serverManager == null)
{
throw new Exception("获取到数据为空!");
}
ApplicationPoolCollection appPools = serverManager.ApplicationPools;
if (appPools == null)
{
throw new Exception("获取到数据为空!");
}
if (appPools.Count <= 0)
{
throw new Exception("iis应用池 无数据!");
}
int count = 0;
foreach (var item in list)
{
count = 0;
foreach (var ap in appPools)
{
string Name = ap.Name;
if (item.name == Name)
{
ObjectState ob = ap.Stop();
count++;
break;
}

}
if (count == 0)
{
throw new Exception("应用池:" + item + " 不存在!");
}
}
MessageBox.Show("停止完成!");
button4_Click(null, null);
}
catch (Exception ex)
{
MessageBox.Show("IIS应用池异常,描述:" + ex.Message);
button4_Click(null, null);
}

}
}
}
class iisITme
{
public string name;
public ObjectState state;
}
}

界面控件代码:
————————————————

namespace 测试IIS应用程序
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.txtIP = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.flpList = new System.Windows.Forms.FlowLayoutPanel();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(489, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "回收";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(22, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 1;
this.label1.Text = "地址:";
//
// txtIP
//
this.txtIP.Location = new System.Drawing.Point(60, 10);
this.txtIP.Name = "txtIP";
this.txtIP.Size = new System.Drawing.Size(83, 21);
this.txtIP.TabIndex = 2;
this.txtIP.Text = "127.0.0.1";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(10, 37);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(53, 12);
this.label2.TabIndex = 4;
this.label2.Text = "应用池:";
//
// button2
//
this.button2.Location = new System.Drawing.Point(570, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 5;
this.button2.Text = "启动";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(651, 8);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 6;
this.button3.Text = "停止";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(177, 8);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(123, 23);
this.button4.TabIndex = 9;
this.button4.Text = "加载应用池列表";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// flpList
//
this.flpList.AutoScroll = true;
this.flpList.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.flpList.Dock = System.Windows.Forms.DockStyle.Fill;
this.flpList.Location = new System.Drawing.Point(3, 17);
this.flpList.Name = "flpList";
this.flpList.Size = new System.Drawing.Size(664, 300);
this.flpList.TabIndex = 10;
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.flpList);
this.groupBox1.Location = new System.Drawing.Point(60, 34);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(670, 320);
this.groupBox1.TabIndex = 11;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "iis";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(742, 366);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtIP);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "IIS应用池测试";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtIP;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.FlowLayoutPanel flpList;
private System.Windows.Forms.GroupBox groupBox1;
}
}

—————————————————————————

源码下载地址:https://download.csdn.net/download/tanglingbo/11490128

版权声明:本文为CSDN博主「唐灵波」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/tanglingbo/article/details/98735819

原文地址:https://www.cnblogs.com/kelelipeng/p/12320850.html

时间: 2024-08-26 16:15:19

C# iis应用程序池控制:回收、启动、停止的相关文章

如何设置IIS程序池的回收时间,才能最大程度的减少对用户的影响?

作为.Net开发人员,其实对IIS的应用程序池知之甚少,前段时间被问到一个问题: 对于互联网web应用,如何在用户毫无感知的情况下回收程序池?(对用户产生最小的影响) 简单理解IIS应用程序池 应用程序池可以看成是装载计算机分配给Web应用程序的内存的容器. 网络上有人这样比喻:如果内存是水,那么应用程序池就是鱼缸,Web应用程序就是鱼缸里的金鱼.多个Web应用程序可以放在同一个应用程序池里面,也就是说一个鱼缸可以养多条金鱼.如果金鱼多了,鱼缸的的空间有限,那么金鱼之间就会争抢生存空间,不是很坚

IIS应用程序池_缓存回收

本人最近由于公司业务,需要把问卷的问题和答案存入缓存中已提高问卷加载速度,减少数据库压力. 缓存关键代码(公司代码已做封装,这里只贴出关键代码): HttpRuntime.Cache.Insert(key, value, new CacheDependency(dependencyFile), Cache.NoAbsoluteExpiration, slidingExpiration, CacheItemPriority.High, onRemoveCallBack); 该缓存存储在了:IIS应

IIS应用程序池相关问题及连接池已满的解决方法

        关于应用程序池 在 IIS 6.0 中,引入了应用程序池,应用程序池是将一个或多个应用程序链接到一个或多个工作进程集合的配置.因为应用程序池中的应用程序与其他应用程序被工作进程边界分隔,所以某个应用程序池中的应用程序不会受到其他应用程序池中应用程序所产生的问题的影响.有了应用程序池,就可以使用被隔离的进程来运行 Web 应用程序.每个应用程序池在服务器上都具有唯一的凭据,因此您可以识别出哪些应用程序在执行哪些操作.如果一个应用程序失败,它不会影响在同时运行的其他应用程序.   

linux下nginx启动停止重启控制脚本

这是控制nginx服务的脚本文件,包括控制nginx的启动.重启.停止.平滑重启.对配置文件的额检查. [[email protected] ~]# cat nginx.sh #!/bin/env bash # description:nginx server   ###必须加描述 # nginx - this script is used to control nginx service # processname nginx # chkconfig: - 85 15 # edit by su

控制Linux下 mono 服务的启动停止

当Window下的服务部署到Linux的时候,我们一般用Mono.service 来启动停止.参数比较多,不太好用.于是有个这个Shell脚本. 用法:moa s1 start #启动           moa s1 stop # 停止   要求有如下的目录结构: /opt/basepath/                     s1/                          sample1.exe #服务1,文件名称不限                     s2/     

服务器IIS应用程序池假死

首先看你的服务开启没有 ASP.NET State Service IIS Admin Service 设置成自动启动 然后设置Internet信息服务(IIS)管理器下的 网站默认网站右键属性调调 或者看看下面的也行: 1:没有打SP1补丁的时候会出现这个IIS6.0假死问题,但现在微软都在自动更新里面出补丁了,一般你打好最新补丁后是不会出现此问题了.(所以现在的IIS假死与这个关系不是很大) 2:从IIS6.0开始CPU资源都在应用池里面限制了,不象以前的IIS.5.所以假死的池的缘故就是池

mysql启动停止,一台服务器跑 多个mysql数据库

一.以非特权用户运行MySQL服务器在讨论如何启动MySQL服务器之前,让我们考虑一下应该以什么用户身份运行MySQL服务器.服务器可以手动或自动启动.如果你手动启动它, 服务器以你登录Unix(Linux)的用户身份启动,即如果你用paul登录Unix并启动服务器,它用paul运行:如果你用su命令切换到root,然后运启动服务器,则它以root运行.然而,大多数情况下你可能不想手动启动服务器,最有可能是你安排MySQL服务器在系统引导时自动启动,作为标准引导过程的一部分,在Unix下,该引导

在CentOS 7中启动/停止/重启服务

RHEL/CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理.systemd兼容SysV和Linux标准组的启动脚本. Systemd是一个Linux操作系统下的系统和服务管理器.它被设计成向后兼容SysV启动脚本,并提供了大量的特性,如开机时平行启动系统服务,按需启动守护进程,支持系统状态快照,或者基于依赖的服务控制逻辑. 先前的使用SysV初始化或Upstart的红帽企业版Linux版本

nginx启动停止命令

操作环境是Windows 一.nginx命令:启动nginx 在Windows上安装好nginx后,我们需要启动nginx服务,启动nginx服务的命令行操作主要有两种方式,即 cd D:\opensource\nginx-1.10.1        nginx.exe 或者 cd D:\opensource\nginx-1.10.1 start nginx 启动nginx命令说明:需要注意,由于nginx默认端口也是80端口,如果此时你的机器上开启了Apache或者IIS服务,切忌在启动ngi