最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#操作IIS方法集合

 更新時(shí)間:2015年09月28日 12:04:56   投稿:lijiao  
這篇文章主要介紹了C#操作IIS方法集合的相關(guān)資料,需要的朋友可以參考下

C# 操作IIS方法集合
如果在win8,win7情況下報(bào)錯(cuò):未知錯(cuò)誤(0x80005000) ----見http://www.fzitv.net/article/72881.htm

using System;
using System.Collections;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace IISControlHelper
{
  /// <summary>
  /// IIS 操作方法集合
  /// http://www.fzitv.net/article/72881.htm 錯(cuò)誤
  /// </summary>
  public class IISWorker
  {
    private static string HostName = "localhost";

    /// <summary>
    /// 獲取本地IIS版本
    /// </summary>
    /// <returns></returns>
    public static string GetIIsVersion()
    {
      try
      {
        DirectoryEntry entry = new DirectoryEntry("IIS://" + HostName + "/W3SVC/INFO");
        string version = entry.Properties["MajorIISVersionNumber"].Value.ToString();
        return version;
      }
      catch (Exception se)
      {
        //說明一點(diǎn):IIS5.0中沒有(int)entry.Properties["MajorIISVersionNumber"].Value;屬性,將拋出異常 證明版本為 5.0
        return string.Empty;
      }
    }

    /// <summary>
    /// 創(chuàng)建虛擬目錄網(wǎng)站
    /// </summary>
    /// <param name="webSiteName">網(wǎng)站名稱</param>
    /// <param name="physicalPath">物理路徑</param>
    /// <param name="domainPort">站點(diǎn)+端口,如192.168.1.23:90</param>
    /// <param name="isCreateAppPool">是否創(chuàng)建新的應(yīng)用程序池</param>
    /// <returns></returns>
    public static int CreateWebSite(string webSiteName, string physicalPath, string domainPort,bool isCreateAppPool)
    {
      DirectoryEntry root = new DirectoryEntry("IIS://" + HostName + "/W3SVC");
      // 為新WEB站點(diǎn)查找一個(gè)未使用的ID
      int siteID = 1;
      foreach (DirectoryEntry e in root.Children)
      {
        if (e.SchemaClassName == "IIsWebServer")
        {
          int ID = Convert.ToInt32(e.Name);
          if (ID >= siteID) { siteID = ID + 1; }
        }
      }
      // 創(chuàng)建WEB站點(diǎn)
      DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);
      site.Invoke("Put", "ServerComment", webSiteName);
      site.Invoke("Put", "KeyType", "IIsWebServer");
      site.Invoke("Put", "ServerBindings", domainPort + ":");
      site.Invoke("Put", "ServerState", 2);
      site.Invoke("Put", "FrontPageWeb", 1);
      site.Invoke("Put", "DefaultDoc", "Default.html");
      // site.Invoke("Put", "SecureBindings", ":443:");
      site.Invoke("Put", "ServerAutoStart", 1);
      site.Invoke("Put", "ServerSize", 1);
      site.Invoke("SetInfo");
      // 創(chuàng)建應(yīng)用程序虛擬目錄

      DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
      siteVDir.Properties["AppIsolated"][0] = 2;
      siteVDir.Properties["Path"][0] = physicalPath;
      siteVDir.Properties["AccessFlags"][0] = 513;
      siteVDir.Properties["FrontPageWeb"][0] = 1;
      siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/" + siteID + "/Root";
      siteVDir.Properties["AppFriendlyName"][0] = "Root";

      if (isCreateAppPool)
      {
        DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools");

        DirectoryEntry newpool = apppools.Children.Add(webSiteName, "IIsApplicationPool");
        newpool.Properties["AppPoolIdentityType"][0] = "4"; //4
        newpool.Properties["ManagedPipelineMode"][0] = "0"; //0:集成模式 1:經(jīng)典模式
        newpool.CommitChanges();
        siteVDir.Properties["AppPoolId"][0] = webSiteName;
      }

      siteVDir.CommitChanges();
      site.CommitChanges();
      return siteID;
    }

    /// <summary>
    /// 得到網(wǎng)站的物理路徑
    /// </summary>
    /// <param name="rootEntry">網(wǎng)站節(jié)點(diǎn)</param>
    /// <returns></returns>
    public static string GetWebsitePhysicalPath(DirectoryEntry rootEntry)
    {
      string physicalPath = "";
      foreach (DirectoryEntry childEntry in rootEntry.Children)
      {
        if ((childEntry.SchemaClassName == "IIsWebVirtualDir") && (childEntry.Name.ToLower() == "root"))
        {
          if (childEntry.Properties["Path"].Value != null)
          {
            physicalPath = childEntry.Properties["Path"].Value.ToString();
          }
          else
          {
            physicalPath = "";
          }
        }
      }
      return physicalPath;
    }

    /// <summary>
    /// 獲取站點(diǎn)名
    /// </summary>
    public static List<IISInfo> GetServerBindings()
    {
      List<IISInfo> iisList = new List<IISInfo>();
      string entPath = String.Format("IIS://{0}/w3svc", HostName);
      DirectoryEntry ent = new DirectoryEntry(entPath);
      foreach (DirectoryEntry child in ent.Children)
      {
        if (child.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase))
        {
          if (child.Properties["ServerBindings"].Value != null)
          {
            object objectArr = child.Properties["ServerBindings"].Value;
            string serverBindingStr = string.Empty;
            if (IsArray(objectArr))//如果有多個(gè)綁定站點(diǎn)時(shí)
            {
              object[] objectToArr = (object[])objectArr;
              serverBindingStr = objectToArr[0].ToString();
            }
            else//只有一個(gè)綁定站點(diǎn)
            {
              serverBindingStr = child.Properties["ServerBindings"].Value.ToString();
            }
            IISInfo iisInfo = new IISInfo();
            iisInfo.DomainPort = serverBindingStr;
            iisInfo.AppPool = child.Properties["AppPoolId"].Value.ToString();//應(yīng)用程序池
            iisList.Add(iisInfo);
          }
        }
      }
      return iisList;
    }


    public static bool CreateAppPool(string appPoolName, string Username, string Password)
    {
      bool issucess = false;
      try
      {
        //創(chuàng)建一個(gè)新程序池
        DirectoryEntry newpool;
        DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools");
        newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");

        //設(shè)置屬性 訪問用戶名和密碼 一般采取默認(rèn)方式
        newpool.Properties["WAMUserName"][0] = Username;
        newpool.Properties["WAMUserPass"][0] = Password;
        newpool.Properties["AppPoolIdentityType"][0] = "3";
        newpool.CommitChanges();
        issucess = true;
        return issucess;
      }
      catch // (Exception ex) 
      {
        return false;
      }
    }


    /// <summary>
    /// 建立程序池后關(guān)聯(lián)相應(yīng)應(yīng)用程序及虛擬目錄
    /// </summary>
    public static void SetAppToPool(string appname,string poolName)
    {
      //獲取目錄
      DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");
      foreach (DirectoryEntry getentity in getdir.Children)
      {
        if (getentity.SchemaClassName.Equals("IIsWebServer"))
        {
          //設(shè)置應(yīng)用程序程序池 先獲得應(yīng)用程序 在設(shè)定應(yīng)用程序程序池
          //第一次測試根目錄
          foreach (DirectoryEntry getchild in getentity.Children)
          {
            if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))
            {
              //找到指定的虛擬目錄.
              foreach (DirectoryEntry getsite in getchild.Children)
              {
                if (getsite.Name.Equals(appname))
                {
                  //【測試成功通過】
                  getsite.Properties["AppPoolId"].Value = poolName;
                  getsite.CommitChanges();
                }
              }
            }
          }
        }
      }
    }


    /// <summary>
    /// 判斷object對象是否為數(shù)組
    /// </summary>
    public static bool IsArray(object o)
    {
      return o is Array;
    }
  }
}

希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • WPF實(shí)現(xiàn)類似ChatGPT逐字打印效果的示例代碼

    WPF實(shí)現(xiàn)類似ChatGPT逐字打印效果的示例代碼

    前一段時(shí)間ChatGPT類的應(yīng)用十分火爆,這類應(yīng)用在回答用戶的問題時(shí)逐字打印輸出,像極了真人打字回復(fù)消息,本文就來利用WPF模擬一下這種逐字打印的效果吧
    2023-08-08
  • Unity攝像機(jī)移至某物體附近觀察此物體

    Unity攝像機(jī)移至某物體附近觀察此物體

    這篇文章主要為大家詳細(xì)介紹了Unity攝像機(jī)移至某物體附近,觀察此物體,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • C#超實(shí)用代碼段合集

    C#超實(shí)用代碼段合集

    這篇文章主要介紹了C#超實(shí)用代碼段合集,涉及C#針對圖標(biāo)、數(shù)學(xué)運(yùn)算、拼音、日期、時(shí)間及文件夾等的相關(guān)操作技巧,需要的朋友可以參考下
    2015-11-11
  • C#中yield return用法分析

    C#中yield return用法分析

    這篇文章主要介紹了C#中yield return用法,對比使用yield return與不使用yield return的流程,更直觀的分析了yield return的用法,需要的朋友可以參考下
    2014-09-09
  • jQuery uploadify在谷歌和火狐瀏覽器上傳失敗的解決方案

    jQuery uploadify在谷歌和火狐瀏覽器上傳失敗的解決方案

    jquery.uploadify插件是一個(gè)基于jquery來實(shí)現(xiàn)上傳的,這個(gè)插件很好用,每一次向后臺(tái)發(fā)送數(shù)據(jù)流請求時(shí),ie會(huì)自動(dòng)把本地cookie存儲(chǔ)捆綁在一起發(fā)送給服務(wù)器。但firefox、chrome不會(huì)這樣做,他們會(huì)認(rèn)為這樣不安全,下面介紹下jQuery uploadify上傳失敗的解決方案
    2015-08-08
  • C#中的值傳遞和引用傳遞詳細(xì)解析

    C#中的值傳遞和引用傳遞詳細(xì)解析

    本篇文章主要是對C#中的引用傳遞與值傳遞進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#如何遍歷Dictionary

    C#如何遍歷Dictionary

    這篇文章主要為大家詳細(xì)介紹了C#遍歷Dictionary的方法,.NET中的Dictionary是鍵/值對的集合,使用起來比較方便,Dictionary也可以用KeyValuePair來迭代遍歷,感興趣的小伙伴們可以參考一下
    2016-04-04
  • C#算法之大牛生小牛的問題高效解決方法

    C#算法之大牛生小牛的問題高效解決方法

    本文主要介紹兩種方法處理大牛生小牛的問題,第二種效率更高,希望能給大家一個(gè)參考。
    2016-06-06
  • 用NPOI創(chuàng)建Excel、合并單元格、設(shè)置單元格樣式、邊框的方法

    用NPOI創(chuàng)建Excel、合并單元格、設(shè)置單元格樣式、邊框的方法

    本篇文章小編為大家介紹,用NPOI創(chuàng)建Excel、合并單元格、設(shè)置單元格樣式、邊框的方法。需要的朋友參考下
    2013-04-04
  • c#實(shí)現(xiàn)無標(biāo)題欄窗口的拖動(dòng)

    c#實(shí)現(xiàn)無標(biāo)題欄窗口的拖動(dòng)

    本篇文章是對c#中實(shí)現(xiàn)無標(biāo)題欄窗口拖動(dòng)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06

最新評論

海原县| 监利县| 阿克陶县| 大渡口区| 桑植县| 梁山县| 高密市| 安徽省| 南澳县| 林西县| 邯郸市| 闻喜县| 织金县| 绿春县| 乌鲁木齐县| 永年县| 大英县| 岳池县| 丹巴县| 资阳市| 德惠市| 尉犁县| 西安市| 金寨县| 哈密市| 新竹市| 抚顺市| 舟曲县| 海宁市| 东城区| 曲水县| 垦利县| 陈巴尔虎旗| 手游| 临清市| 治多县| 邵阳县| 大丰市| 梁河县| 苍梧县| 福海县|