ローカルの iis にアクセスし、iis の Web サイト (ほとんどが SharePoint 2007 サイト) にアプリケーション プールをプログラムで作成/削除/割り当てできる Web アプリケーションを作成しようとしています。作成と削除は機能していますが、割り当ては機能していません。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();
}