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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值