操作IIS的应用程序池与站点分配(C#)-创新互联

一个应用程序池可以有多个站点,一个站点只对应一个应用程序池。

创新互联主要从事成都网站制作、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务边坝,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

编程由来:

    存放在一个应用程序池里的站点过多就不便于操作,所以需把其中一些站点分配到其他程序池中。

编程题目:

   用户输入一个数字或者一个数字+一个名字。程序对站点的所在应用程序池进行统计,用户输入的数字用于限制应用程序池里面的大容量数,如果超出该容量,将把超出的站点分配到其他程序应用池,或者新建的一个应用程序池,把站点分配进去。

如果用户输入一个数字的情况,将遍历所有程序应用池;如果用户输入一个数字+一个名字的情况,将只对该名字的应用程序池进行操作;如果站点的名字和应用程序池的名字一样,将不进行操作。

条件:

一、把DefautlAppPool应用程序池或者含有字符"AppPool #"的应用程序池里面的超出的站点分配到AppPool #?应用程序池中("?"代表数字)

二、如果aspnet1应用程序池里面的网站数超出用户限制的数字,则分配到新建应用程序池的命名方式为aspnet1-?;("?"代表数字,表示从属aspnet1下的分支)

三、如二设置aspnet2,aspnet3,aspnet4应用程序池

四、当网站名字和应用程序池的名字相同时,将不进行操作

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

这是我在公司里面的任务,以下是我公开的代码(还不属于最优化的代码,我把我的制作品拿出来以代表原创性,最优化的代码暂时不公布,如有需要,请联系博主!)

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

新建一个控制台应用程序(C#编程语言,使用vs2005版本制作)

添加引用:System.DirectoryServices

class Program

  {

    static Hashtable hs = new Hashtable();//创建哈希表,保存池中的站点

    static string[] pls;//池数组

    static string[] nums;//应用程序池中各自包含的网站数量

    static Hashtable boolhs = new Hashtable();//创建哈希表,保存池中站点数量是否满

    static void Main(string[] args)

    {

      string strNum = Console.ReadLine();//用户输入信息

      pls = GetApplicationPools();//获取应用程序池名称数组

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

      {

        hs.Add(i, "");

        boolhs.Add(i, "false");

      }

      getpoolweb();

      WebNums();

      if (strNum.Length > 1)//判断用户输入的数字+名称

      {

        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";//将该池定义站点数量已满

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

              Console.WriteLine("编译完毕!");

            }

            else Console.WriteLine("该"+pw[1].ToString()+"应用程序池不需进行操作!");

          }

        }

      }

      else//判断用户输入的数字

      {

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

        {

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

          {

            boolhs[pls[i]] = "true";//将该池定义站点数量已满

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

            Console.WriteLine("编译完毕!");

          }

        }

      }

      Console.ReadLine();

    }

    /// <summary>

    /// 判断网站名与应用程序池名称是否相等

    /// </summary>

    /// <param name="wnames">网站名称</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>

    /// 获得池数组对应的网站数量

    /// </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>

    ///检测应用程序池的名称

    ///</summary>

    ///<param name="AppPoolName">应用程序池的名称</param>

    ///<param name="c">指定的限制数</param>

    ///<param name="inn">该池中网站的数量</param>

    ///<returns></returns>

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

    {

      int si = inn - c;//旧池中站点剩余量

      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")//如果哪个池的站点量可以容纳

          {

            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]);//转移站点

            }

            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());//新建多少个

        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);//转移站点

              }

            }

            //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 池与网站的操作(获得所有池;获得指定池的网站名称;移动网站到新池)

    /// <summary>

    /// 获取应用程序池->数组

    /// </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>

    /// 获得所有的应用程序池和对应站点

    /// </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">应用程序池名称</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();//添加新池到数组中

        WebNums();

        boolhs.Add(AppPoolName, "false");

        return findPool;

      }

      catch (Exception ex)

      {

        Console.WriteLine(ex.Message);

        return null;

      }

    }

    /// <summary>

    /// 移动网站到新池

    /// </summary>

    /// <param name="webns">网站名称</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++)//遍历旧池并修改原数目数组的数据

        {

          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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网站题目:操作IIS的应用程序池与站点分配(C#)-创新互联
当前网址:https://www.cdcxhl.com/article48/dhhdhp.html

成都网站建设公司_创新互联,为您提供电子商务网页设计公司网站改版面包屑导航虚拟主机域名注册

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

微信小程序开发