方法一:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.DirectoryServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Management;
using System.Text;
using System.Windows.Forms;
namespace TDHost
{
public partial class ResatartIIS : Form
{
/// <summary>
/// 连接池的名字
/// </summary>
string stringAppPoolName = "";
/// <summary>
/// 连接池操作的开启,关闭,回收
/// </summary>
string stringmethod = "";
public ResatartIIS()
{
InitializeComponent();
}
private void iisStart_Click(object sender, EventArgs e)
{
//如果应用程序池不存在,则会报错系统找不到指定路径
stringAppPoolName = txtIISAppPool.Text.Trim();
stringmethod = "Start";
try
{
DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
DirectoryEntry findPool = appPool.Children.Find(stringAppPoolName, "IIsApplicationPool");
findPool.Invoke(stringmethod, null);
appPool.CommitChanges();
appPool.Close();
MessageBox.Show("应用程序池名称启动成功", "启动成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "启动失败");
}
}
private void iisStop_Click(object sender, EventArgs e)
{
//如果应用程序池当前状态为停止,则会发生异常报错
stringAppPoolName = txtIISAppPool.Text.Trim();
stringmethod = "Recycle";
try
{
DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
DirectoryEntry findPool = appPool.Children.Find(stringAppPoolName, "IIsApplicationPool");
findPool.Invoke(stringmethod, null);
appPool.CommitChanges();
appPool.Close();
MessageBox.Show("应用程序池名称回收成功", "回收成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "回收失败");
}
}
private void iisInvoke_Click(object sender, EventArgs e)
{
stringAppPoolName = txtIISAppPool.Text.Trim();
stringmethod = "Stop";
try
{
DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
DirectoryEntry findPool = appPool.Children.Find(stringAppPoolName, "IIsApplicationPool");
findPool.Invoke(stringmethod, null);
appPool.CommitChanges();
appPool.Close();
MessageBox.Show("应用程序池名称停止成功", "停止成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "停止失败");
}
}
}
}
方法二:
WIN2008:%windir%\System32\inetsrv\appcmd.exe recycle APPPOOL /apppool.name:应用池名
WIN2003:%windir%\7i24.com\FreeHost\wakeupiis.exe -s -poolname 应用池名
显示连接池列表: %windir%\System32\inetsrv\appcmd.exe list apppool