中文字幕日韩精品一区二区免费_精品一区二区三区国产精品无卡在_国精品无码专区一区二区三区_国产αv三级中文在线

操作IIS的應(yīng)用程序池與站點(diǎn)分配(C#)-創(chuàng)新互聯(lián)

一個(gè)應(yīng)用程序池可以有多個(gè)站點(diǎn),一個(gè)站點(diǎn)只對(duì)應(yīng)一個(gè)應(yīng)用程序池。

創(chuàng)新互聯(lián)主要從事成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)邊壩,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575

編程由來:

    存放在一個(gè)應(yīng)用程序池里的站點(diǎn)過多就不便于操作,所以需把其中一些站點(diǎn)分配到其他程序池中。

編程題目:

   用戶輸入一個(gè)數(shù)字或者一個(gè)數(shù)字+一個(gè)名字。程序?qū)φ军c(diǎn)的所在應(yīng)用程序池進(jìn)行統(tǒng)計(jì),用戶輸入的數(shù)字用于限制應(yīng)用程序池里面的大容量數(shù),如果超出該容量,將把超出的站點(diǎn)分配到其他程序應(yīng)用池,或者新建的一個(gè)應(yīng)用程序池,把站點(diǎn)分配進(jìn)去。

如果用戶輸入一個(gè)數(shù)字的情況,將遍歷所有程序應(yīng)用池;如果用戶輸入一個(gè)數(shù)字+一個(gè)名字的情況,將只對(duì)該名字的應(yīng)用程序池進(jìn)行操作;如果站點(diǎn)的名字和應(yīng)用程序池的名字一樣,將不進(jìn)行操作。

條件:

一、把DefautlAppPool應(yīng)用程序池或者含有字符"AppPool #"的應(yīng)用程序池里面的超出的站點(diǎn)分配到AppPool #?應(yīng)用程序池中("?"代表數(shù)字)

二、如果aspnet1應(yīng)用程序池里面的網(wǎng)站數(shù)超出用戶限制的數(shù)字,則分配到新建應(yīng)用程序池的命名方式為aspnet1-?;("?"代表數(shù)字,表示從屬aspnet1下的分支)

三、如二設(shè)置aspnet2,aspnet3,aspnet4應(yīng)用程序池

四、當(dāng)網(wǎng)站名字和應(yīng)用程序池的名字相同時(shí),將不進(jìn)行操作

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

這是我在公司里面的任務(wù),以下是我公開的代碼(還不屬于最優(yōu)化的代碼,我把我的制作品拿出來以代表原創(chuàng)性,最優(yōu)化的代碼暫時(shí)不公布,如有需要,請(qǐng)聯(lián)系博主?。?/p>

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

新建一個(gè)控制臺(tái)應(yīng)用程序(C#編程語言,使用vs2005版本制作)

添加引用:System.DirectoryServices

class Program

  {

    static Hashtable hs = new Hashtable();//創(chuàng)建哈希表,保存池中的站點(diǎn)

    static string[] pls;//池?cái)?shù)組

    static string[] nums;//應(yīng)用程序池中各自包含的網(wǎng)站數(shù)量

    static Hashtable boolhs = new Hashtable();//創(chuàng)建哈希表,保存池中站點(diǎn)數(shù)量是否滿

    static void Main(string[] args)

    {

      string strNum = Console.ReadLine();//用戶輸入信息

      pls = GetApplicationPools();//獲取應(yīng)用程序池名稱數(shù)組

      foreach (string i in pls)//填充哈希表key值內(nèi)容

      {

        hs.Add(i, "");

        boolhs.Add(i, "false");

      }

      getpoolweb();

      WebNums();

      if (strNum.Length > 1)//判斷用戶輸入的數(shù)字+名稱

      {

        string[] pw = strNum.Split(' ');

        for (int i = 0; i < pls.Length; i++)

        {

          if (pls[i] == pw[1])

          {

            if (int.Parse(nums[i]) > int.Parse(pw[0]))

            {

              boolhs[pls[i]] = "true";//將該池定義站點(diǎn)數(shù)量已滿

              GetName(pw[1], int.Parse(pw[0]), int.Parse(nums[i]));

              Console.WriteLine("編譯完畢!");

            }

            else Console.WriteLine("該"+pw[1].ToString()+"應(yīng)用程序池不需進(jìn)行操作!");

          }

        }

      }

      else//判斷用戶輸入的數(shù)字

      {

        for (int i = 0; i < pls.Length; i++)

        {

          if (int.Parse(nums[i]) > int.Parse(strNum))//如果超出

          {

            boolhs[pls[i]] = "true";//將該池定義站點(diǎn)數(shù)量已滿

            GetName(pls[i], int.Parse(strNum), int.Parse(nums[i]));

            Console.WriteLine("編譯完畢!");

          }

        }

      }

      Console.ReadLine();

    }

    /// <summary>

    /// 判斷網(wǎng)站名與應(yīng)用程序池名稱是否相等

    /// </summary>

    /// <param name="wnames">網(wǎng)站名稱</param>

    /// <returns>相等為假</returns>

    public static bool chname(string wnames)

    {

      bool ctf = true;

      foreach (string i in pls)

      {

        if (wnames == i)

          ctf = false;

        else ctf = true;

      }

      return ctf;

    }

    /// <summary>

    /// 獲得池?cái)?shù)組對(duì)應(yīng)的網(wǎng)站數(shù)量

    /// </summary>

    static void WebNums()

    {

      List<string> weblist = new List<string>();

      //string[] poolns = pooln.Split(',');

      foreach (string i in pls)

      {

        if (hs[i].ToString() != "")

          weblist.Add(hs[i].ToString().Split(',').Length.ToString());

        else

          weblist.Add("0");

      }

      nums = weblist.ToArray();

    }

    ///<summary>

    ///檢測應(yīng)用程序池的名稱

    ///</summary>

    ///<param name="AppPoolName">應(yīng)用程序池的名稱</param>

    ///<param name="c">指定的限制數(shù)</param>

    ///<param name="inn">該池中網(wǎng)站的數(shù)量</param>

    ///<returns></returns>

    static void GetName(string AppPoolName, int c, int inn)

    {

      int si = inn - c;//舊池中站點(diǎn)剩余量

      string[] kt = hs[AppPoolName].ToString().Split(',');

      while (true)

      {

        int ting = 0;

        foreach (string w in pls)

          if (boolhs[w].ToString() == "true")

            ting += 1;

        if (ting >= pls.Length) break;

        for (int i = 0; i < pls.Length; i++)

        {

          if (boolhs[pls[i]].ToString() == "false")//如果哪個(gè)池的站點(diǎn)量可以容納

          {

            int d = c - int.Parse(nums[i]);

            if (si < c)

            {

              for (int j = 0; j < si; j++)

               if (chname(kt[j]))//判斷名稱是否存在

                 movepool(kt[j], AppPoolName, pls[i]);//轉(zhuǎn)移站點(diǎn)

            }

            else

            {

              for (int j = 0; j < d; j++)

                if (chname(kt[j]))

                  movepool(kt[j], AppPoolName, pls[i]);

            }

            if (si-d < 0) break;

            si = si - d;

            boolhs[pls[i]] = "true";

          }

        }

      }

      //需要新建的情況

      if(si>0)

      {

        int sy = int.Parse(Math.Ceiling((double)si / (double)c).ToString());//新建多少個(gè)

        for (int j = 1; j <= sy; j++)

        {

          string ne = "";

          bool bname = false;

          int s = 1;

          while (bname == false)

          {

            if (AppPoolName.StartsWith("aspnet")) ne = AppPoolName + "-" + s;

            else if (AppPoolName.StartsWith("DefaultAppPool") && AppPoolName.StartsWith("AppPool #")) ne = AppPoolName + s;

            bool bne = false;//判斷名稱是否存在

            foreach (string n in pls)

            {

              if (n == ne)

              {

                bne = true;

                break;

              }

            }

            if (bne == true)

              s += 1;

            else bname = true;

          }

          AddAppPool(ne);//新建池

          for (int i = 0; i < c ; i++)

          {

            if (i < si)

            {

              if (chname(kt[i]))//判斷名稱是否存在

              {

                movepool(kt[i], AppPoolName, ne);//轉(zhuǎn)移站點(diǎn)

              }

            }

            //if (si < c)

            //{

            //   for (int j = 0; j < si; j++)

            //     movepool(kt[j], AppPoolName, pls[i]);

            //}

            //else

            //{

            //   for (int j = 0; j < d; j++)

            //     movepool(kt[j], AppPoolName, pls[i]);

            //}

          }

          si = si - c;

        }

      }

    }

    #region 池與網(wǎng)站的操作(獲得所有池;獲得指定池的網(wǎng)站名稱;移動(dòng)網(wǎng)站到新池)

    /// <summary>

    /// 獲取應(yīng)用程序池->數(shù)組

    /// </summary>

    /// <returns></returns>

    public static string[] GetApplicationPools()

    {

      DirectoryEntry directoryEntry = new DirectoryEntry("IIS://LOCALHOST/W3SVC/AppPools");

      if (directoryEntry == null) return null;

      List<string> list = new List<string>();

      foreach (DirectoryEntry entry2 in directoryEntry.Children)

      {

        PropertyCollection properties = entry2.Properties;

        list.Add(entry2.Name.ToString().Trim());

      }

      return list.ToArray();

    }

    /// <summary>

    /// 獲得所有的應(yīng)用程序池和對(duì)應(yīng)站點(diǎn)

    /// </summary>

    static void getpoolweb()

    {

      DirectoryEntry root = null;

      try

      {

        root = new DirectoryEntry("IIS://localhost/W3SVC");

      }

      catch

      {

        return;

      }

      foreach (DirectoryEntry website in root.Children)

      {

        try

        {

          if (website.SchemaClassName != "IIsWebServer") continue;

          string comment = website.Properties["ServerComment"][0].ToString();

          DirectoryEntry siteVDir = website.Children.Find("Root", "IISWebVirtualDir");

          string poolname = "";

          try

          {

            poolname = siteVDir.Properties["AppPoolId"][0].ToString().Trim();

          }

          catch (Exception ex)

          {

            Console.WriteLine(ex.Message);

          }

          if (poolname == "")

          {

            try

            {

              poolname = website.Properties["AppPoolId"][0].ToString().Trim();

            }

            catch (Exception ex)

            {

              Console.WriteLine(ex.Message);

            }

          }

          //if (pooln == "") pooln = poolname;

          //else pooln += "," + poolname;

          //string[] poolns = pooln.Split(',');

          foreach (string i in pls)

          {

            if (i == poolname)

            {

              if (hs[i].ToString() == "")

                hs[i] = comment;

              else hs[i] += "," + comment;

            }

          }

        }

        catch (Exception ex)

        {

          Console.WriteLine(ex.Message);

        }

      }

      root.Close();

    }

    /// <summary>

    /// 新建池

    /// </summary>

    /// <param name="AppPoolName">應(yīng)用程序池名稱</param>

    /// <returns></returns>

    public static DirectoryEntry AddAppPool(string AppPoolName)

    {

      try

      {

        DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");

        DirectoryEntry findPool = null;

        try

        {

          findPool = appPool.Children.Find(AppPoolName, "IIsApplicationPool");

        }

        catch (Exception) { }

        if (findPool == null)

        {

          findPool = appPool.Children.Add(AppPoolName, "IIsApplicationPool");

          findPool.CommitChanges();

          appPool.CommitChanges();

        }

        //pooln += "," + AppPoolName;

        List<string> a = new List<string>();

        foreach (string b in pls)

          a.Add(b);

        a.Add(AppPoolName);

        pls = a.ToArray();//添加新池到數(shù)組中

        WebNums();

        boolhs.Add(AppPoolName, "false");

        return findPool;

      }

      catch (Exception ex)

      {

        Console.WriteLine(ex.Message);

        return null;

      }

    }

    /// <summary>

    /// 移動(dòng)網(wǎng)站到新池

    /// </summary>

    /// <param name="webns">網(wǎng)站名稱</param>

    /// <param name="poolold">舊池名稱</param>

    /// <param name="poolns">新池名稱</param>

    static void movepool(string webns,string poolold, string poolns)

    {

      try

      {

        DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");

        foreach (DirectoryEntry website in root.Children)

        {

          if (website.SchemaClassName != "IIsWebServer") continue;

          string comment = website.Properties["ServerComment"][0].ToString();

          if (comment == webns)

          {

            DirectoryEntry siteVDir = website.Children.Find("Root", "IISWebVirtualDir");

            siteVDir.Invoke("Put", new object[2] { "AppPoolId", poolns });

            siteVDir.CommitChanges();

            website.Invoke("Put", new object[2] { "AppPoolId", poolns });

            website.CommitChanges();

          }

        }

        for (int i = 0; i < pls.Length; i++)//遍歷舊池并修改原數(shù)目數(shù)組的數(shù)據(jù)

        {

          if (pls[i] == poolold)

          {

            nums[i] = (int.Parse(nums[i]) - 1).ToString();

            string[] h = hs[poolold].ToString().Split(',');

            string hnew = "";

            foreach (string s in h)

              if (s != webns)

              {

                if (hnew == "")

                  hnew = s;

                else hnew += "," + s;

              }

            hs[poolold] = hnew;

            if (hs[poolns].ToString() == "") hs[poolns] = webns;

            else hs[poolns] += "," + webns;

          }

          if (pls[i] == poolns)

          {

            WebNums();

            nums[i] = (int.Parse(nums[i]) + 1).ToString();

          }

        }

      }

      catch (Exception ex)

      {

        Console.WriteLine(ex.Message);

      }

    }

    #endregion

  }

附件:http://down.51cto.com/data/2362946

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站題目:操作IIS的應(yīng)用程序池與站點(diǎn)分配(C#)-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://www.rwnh.cn/article48/dhhdhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站改版、面包屑導(dǎo)航、虛擬主機(jī)、域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)
宁德市| 鹤山市| 长葛市| 姚安县| 于都县| 萍乡市| 绥芬河市| 军事| 会理县| 山东省| 大石桥市| 名山县| 凤山县| 峨眉山市| 广河县| 安西县| 淮阳县| 井陉县| 永修县| 东安县| 平远县| 含山县| 龙口市| 闽清县| 轮台县| 奈曼旗| 休宁县| 弥勒县| 巴林右旗| 长垣县| 宜阳县| 西华县| 辽源市| 东明县| 九江市| 新和县| 澎湖县| 麻江县| 大竹县| 富锦市| 蒲城县|