怎么样使用代码管理IIS应用程序池?
using System;
using System.DirectoryServices;
using System.Reflection;
namespace ADSI1
{
///
/// Small class containing methods to configure IIS.
///
class ConfigIIS
{
///
/// The main entry point for the application.
///
[STAThread]
//主程序入口,可以选择用哪些,我为了方便,全部功能都写上去了。
static void Main(string[] args)
{
string AppPoolName = "MyAppPool";
string newvdir1 = "MyVDir";
DirectoryEntry newvdir = CreateVDir(newvdir1);
CreateAppPool(AppPoolName);
AssignAppPool(newvdir, AppPoolName);
ConfigAppPool("Stop",AppPoolName);
}
//创建虚拟目录
static DirectoryEntry CreateVDir (string vdirname)
{
DirectoryEntry newvdir;
DirectoryEntry root=new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
newvdir=root.Children.Add(vdirname, "IIsWebVirtualDir");
newvdir.Properties["Path"][0]= "c://inetpub//wwwroot";
newvdir.Properties["AccessScript"][0] = true;
newvdir.CommitChanges();
return newvdir;
}
//创建新的应用程序池。
static void CreateAppPool(string AppPoolName)
{
DirectoryEntry newpool;
DirectoryEntry apppools=new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
newpool=apppools.Children.Add(AppPoolName, "IIsApplicationPool");
newpool.CommitChanges();
}
static void AssignAppPool(DirectoryEntry newvdir, string AppPoolName)
{
object[] param={0, AppPoolName, true};
newvdir.Invoke("AppCreate3", param);
}
//method是管理应用程序池的方法,有三种Start、Stop、Recycle,而AppPoolName是应用程序池名称
static void ConfigAppPool(string method,string AppPoolName)
{
DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
DirectoryEntry findPool = appPool.Children.Find(AppPoolName,IIsApplicationPool");
findPool.Invoke(method,null);
appPool.CommitChanges();
appPool.Close();
}
//应用程序池的列表
static void AppPoolList()
{
DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
foreach(DirectoryEntry a in appPool.Children)
{
Console.WriteLine(a.Name);
}
}
private void VDirToAppPool()
{
DirectroryEntry VD = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT/ccc");
Console.WriteLine(VD.Properties["AppPoolId"].Value.ToString());
}
}
}
本文地址:http://www.45fan.com/bcdm/70749.html