龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

C#操作IIS 7.0详细代码(2)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
然后是对虚拟目录的操作,包括创建和删除虚拟目录,都比较简单。 CreateVDir public static void CreateVDir(string siteName, string vDirName, string physicalPath) { using (Serv

    然后是对虚拟目录的操作,包括创建和删除虚拟目录,都比较简单。

CreateVDir
public static void CreateVDir(string siteName, string vDirName, string physicalPath)
{
    using (ServerManager mgr = new ServerManager())
    {
        Site site = mgr.Sites[siteName];
        if (site == null)
        {
            throw new ApplicationException(String.Format("Web site {0} does not exist", siteName));
        }
        site.Applications.Add("/" + vDirName, physicalPath);
        mgr.CommitChanges();
    }
}
DeleteVDir
public static void DeleteVDir(string siteName, string vDirName)
{   
    using (ServerManager mgr = new ServerManager())
    {
        Site site = mgr.Sites[siteName];
        if (site != null)
        {
            Microsoft.Web.Administration.Application app = site.Applications["/" + vDirName];
            if (app != null)
            {
                site.Applications.Remove(app);
                mgr.CommitChanges();
            }
        }
    }
}
    删除应用程序池。

DeletePool
/// <summary>
/// Delete an existent web site app pool.
/// </summary>
/// <param name="appPoolName">App pool name for deletion.</param>
public static void DeletePool(string appPoolName)
{
    using (ServerManager mgr = new ServerManager())
    {
        ApplicationPool pool = mgr.ApplicationPools[appPoolName];
        if (pool != null)
        {
            mgr.ApplicationPools.Remove(pool);
            mgr.CommitChanges();
        }
    }
}
    在站点上添加默认文档。

AddDefaultDocument
public static void AddDefaultDocument(string siteName, string defaultDocName)
{
    using (ServerManager mgr = new ServerManager())
    {
        Configuration cfg = mgr.GetWebConfiguration(siteName);
        ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument");
        ConfigurationElement filesElement = defaultDocumentSection.GetChildElement("files");
        ConfigurationElementCollection filesCollection = filesElement.GetCollection();

        foreach (ConfigurationElement elt in filesCollection)
        {
            if (elt.Attributes["value"].Value.ToString() == defaultDocName)
            {
                return;
            }
        }

        try
        {
            ConfigurationElement docElement = filesCollection.CreateElement();
            docElement.SetAttributeValue("value", defaultDocName);
            filesCollection.Add(docElement);
        }
        catch (Exception) { }   //this will fail if existing

        mgr.CommitChanges();
    }
}


精彩图集

赞助商链接