国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#中操作IIS 7.0

2019-11-17 04:18:04
字體:
來源:轉載
供稿:網友

    Microsoft自Windows Vista一起發布了IIS 7.0,這個已經是去年的話題了,隨后,由.NET開發的Web程序便逐步從IIS 6.0過渡到IIS 7.0上了。IIS 7.0提供了很多比上一版本更多的新特性,包括完全模塊化的組件、文本文件的配置功能、MMC圖形模式管理工具等等,并且與.NET編程語言結合得更加緊密了,在新添加的Microsoft.Web.Administration名稱空間中也增加了很多用于管理和訪問IIS的對象,從而使得通過編程方式操作IIS更加簡便。雖然在IIS 6.0時代我們也可以非常輕松地通過C#來管理服務器的IIS,但相對來說,現在需要編寫的代碼更少,所能完成的功能更強。以下是我在曾經做的一個項目中所寫的一個類庫中的一部分,主要實現了對IIS 7.0的操作,包括創建和刪除站點、創建和刪除虛擬目錄、創建和刪除應用程序池、添加站點默認文檔、判斷站點和虛擬目錄是否存在、以及檢查Bindings信息等。

    對于IIS 7.0的介紹讀者如果有興趣的話可以看看下面的兩篇文章,我覺得不錯!

http://blog.joycode.com/scottgu/archive/2007/04/08/100650.aspx

http://msdn.microsoft.com/en-us/magazine/cc163453.aspx

    不說廢話了,趕緊貼代碼吧。

    首先是對站點的管理。我寫了一個相對較為通用的私有方法,然后在對外的方法中給出了調用接口,包括了創建站點時應用程序池的創建和權限的管理。

CreateSite
/// <summary>
/// Create a new web site.
/// </summary>
/// <param name="siteName"></param>
/// <param name="bindingInfo">"*:&lt;port&gt;:&lt;hostname&gt;" <example>"*:80:myhost.com"</example></param>
/// <param name="physicalPath"></param>
public static void CreateSite(string siteName, string bindingInfo, string physicalPath)
{
    createSite(siteName, "http", bindingInfo, physicalPath, true, siteName + "Pool", PRocessModelIdentityType.NetworkService, null, null, ManagedPipelineMode.Integrated, null);
}

private static void createSite(string siteName, string protocol, string bindingInformation, string physicalPath,
        bool createAppPool, string appPoolName, ProcessModelIdentityType identityType,
        string appPoolUserName, string appPoolPassWord, ManagedPipelineMode appPoolPipelineMode, string managedRuntimeVersion)
{
    using (ServerManager mgr = new ServerManager())
    {
        Site site = mgr.Sites.Add(siteName, protocol, bindingInformation, physicalPath);

        // PROVISION APPPOOL IF NEEDED
        if (createAppPool)
        {
            applicationPool pool = mgr.ApplicationPools.Add(appPoolName);
            if (pool.ProcessModel.IdentityType != identityType)
            {
                pool.ProcessModel.IdentityType = identityType;
            }
            if (!String.IsNullOrEmpty(appPoolUserName))
            {
                pool.ProcessModel.UserName = appPoolUserName;
                pool.ProcessModel.Password = appPoolPassword;
            }
            if (appPoolPipelineMode != pool.ManagedPipelineMode)
            {
                pool.ManagedPipelineMode = appPoolPipelineMode;
            }

            site.Applications["/"].ApplicationPoolName = pool.Name;
        }

        mgr.CommitChanges();
    }
}
     這個是刪除站點的方法,比較簡單。

DeleteSite
/// <summary>
/// Delete an existent web site.
/// </summary>
/// <param name="siteName">Site name.</param>
public static void DeleteSite(string siteName)
{
    using (ServerManager mgr = new ServerManager())
    {
        Site site = mgr.Sites[siteName];
        if (site != null)
        {
            mgr.Sites.Remove(site);
            mgr.CommitChanges();
        }
    }
}
    然后是對虛擬目錄的操作,包括創建和刪除虛擬目錄,都比較簡單。

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();
    }
}
     檢查虛擬目錄是否存在。

VerifyVirtualPathIsExist
public static bool VerifyVirtualPathIsExist(string siteName, string path)
{
    using (ServerManager mgr = new ServerManager())
    {
        Site site = mgr.Sites[siteName];
        if (site != null)
        {
            foreach (Microsoft.Web.Administration.Application app in site.Applications)
            {
                if (app.Path.ToUpper().Equals(path.ToUpper()))
                {
                    return true;
                }
            }
        }
    }

    return false;
}
     檢查站點是否存在。

VerifyWebSiteIsExist
public static bool VerifyWebSiteIsExist(string siteName)
{
    using (ServerManager mgr = new ServerManager())
    {
        for (int i = 0; i < mgr.Sites.Count; i++)
        {
            if (mgr.Sites[i].Name.ToUpper().Equals(siteName.ToUpper()))
            {
                return true;
            }
        }
    }

    return false;
}
     檢查Bindings信息。

VerifyWebSiteBindingsIsExist
public static bool VerifyWebSiteBindingsIsExist(string bindingInfo)
{
    string temp = string.Empty;
    using (ServerManager mgr = new ServerManager())
    {
        for (int i = 0; i < mgr.Sites.Count; i++)
        {
            foreach (Microsoft.Web.Administration.Binding b in mgr.Sites[i].Bindings)
            {
                temp = b.BindingInformation;
                if (temp.IndexOf('*') < 0)
                {
                    temp = "*" + temp;
                }
                if (temp.Equals(bindingInfo))
                {
                    return true;
                }
            }
        }
    }

    return false;
}
     以上代碼均在Windows Vista SP1和Windows Server 2008上測試通過,使用時需要在工程中引用Microsoft.Web.Administration類庫,該類庫為IIS 7.0自帶的。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通山县| 古丈县| 九江市| 尼木县| 临泽县| 舟曲县| 鄱阳县| 松原市| 五台县| 梁河县| 万年县| 花垣县| 色达县| 阿拉善左旗| 武强县| 商城县| 新巴尔虎右旗| 阆中市| 濉溪县| 衡东县| 噶尔县| 商都县| 富锦市| 扎鲁特旗| 若尔盖县| 哈巴河县| 精河县| 鲁山县| 吉安市| 将乐县| 井冈山市| 克拉玛依市| 靖西县| 全椒县| 正宁县| 苍梧县| 达日县| 万宁市| 永胜县| 那曲县| 景泰县|