我正在尝试创建一个可以访问本地iis的Web应用程序,并以编程方式创建/删除/分配应用程序池到iis中的网站(大多数是SharePoint 2007站点)。创建和删除正在工作但分配不是。我在Windows Server 2003上使用WSS 3.0,iis 6。 我的代码在这里:
protected void btnChangePool_Click(object sender, EventArgs e)
{
string siteId = GetWebSiteId(ddlSites.SelectedValue);
//ddlSites contains name of site as it shows in iis under websites
DirectoryEntry oldAppPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools/" + lblAppPool.Text);
//lblAppPool contains name of currently app pool associated to our site
if (chkDeleteOld.Checked)
{
using (DirectoryEntry appPool = oldAppPool)
{
using (DirectoryEntry parent = oldAppPool.Parent)
{
parent.Children.Remove(oldAppPool);
parent.CommitChanges();
}
}
}
DirectoryEntry VDir = new DirectoryEntry("IIS://localhost/W3SVC/"+siteId+ "/ROOT");
VDir.Properties["AppPoolId"].Value = ddlAppPools.SelectedValue;
//ddlappPools contains name of new app pool (to be assigned)
VDir.CommitChanges();
}
public string GetWebSiteId(string websiteName)
{
string result = "-1";
DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
foreach (DirectoryEntry site in w3svc.Children)
{
if (site.Properties["ServerComment"] != null)
{
if (site.Properties["ServerComment"].Value != null)
{
if (string.Compare(site.Properties["ServerComment"].Value.ToString(), websiteName,false) == 0)
{
result = site.Name;
break;
}
}
}
}
return result;
}
protected void btnCreatePool_Click(object sender, EventArgs e)
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
DirectoryEntry newAppPool;
//newAppPool = root.Invoke("Create", "IIsApplicationPool", txtCreatePool.Text) as DirectoryEntry;
//OR
newAppPool = root.Children.Add(txtCreatePool.Text, "IIsApplicationPool");
newAppPool.CommitChanges();
root.CommitChanges();
}