2

我正在尝试制作一个 Web 应用程序,它可以访问本地 iis 并以编程方式创建/删除/分配应用程序池给 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();
    } 
4

0 回答 0