设为首页 收藏本站
查看: 840|回复: 0

[经验分享] 操作IIS的应用程序池与站点分配(C#)

[复制链接]

尚未签到

发表于 2018-12-9 06:12:28 | 显示全部楼层 |阅读模式
  一个应用程序池可以有多个站点,一个站点只对应一个应用程序池。
  编程由来:
  存放在一个应用程序池里的站点过多就不便于操作,所以需把其中一些站点分配到其他程序池中。
  编程题目:
  用户输入一个数字或者一个数字+一个名字。程序对站点的所在应用程序池进行统计,用户输入的数字用于限制应用程序池里面的最大容量数,如果超出该容量,将把超出的站点分配到其他程序应用池,或者新建的一个应用程序池,把站点分配进去。
  如果用户输入一个数字的情况,将遍历所有程序应用池;如果用户输入一个数字+一个名字的情况,将只对该名字的应用程序池进行操作;如果站点的名字和应用程序池的名字一样,将不进行操作。
  条件:
  一、把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 == pw[1])
  {
  if (int.Parse(nums) > int.Parse(pw[0]))
  {
  boolhs[pls] = &quot;true&quot;;//将该池定义站点数量已满
  GetName(pw[1], int.Parse(pw[0]), int.Parse(nums));
  Console.WriteLine(&quot;编译完毕!&quot;);
  }
  else Console.WriteLine(&quot;该&quot;+pw[1].ToString()+&quot;应用程序池不需进行操作!&quot;);
  }
  }
  }
  else//判断用户输入的数字
  {
  for (int i = 0; i < pls.Length; i++)
  {
  if (int.Parse(nums) > int.Parse(strNum))//如果超出
  {
  boolhs[pls] = &quot;true&quot;;//将该池定义站点数量已满
  GetName(pls, int.Parse(strNum), int.Parse(nums));
  Console.WriteLine(&quot;编译完毕!&quot;);
  }
  }
  }
  Console.ReadLine();
  }
  ///
  /// 判断网站名与应用程序池名称是否相等
  ///
  /// 网站名称
  /// 相等为假
  public static bool chname(string wnames)
  {
  bool ctf = true;
  foreach (string i in pls)
  {
  if (wnames == i)
  ctf = false;
  else ctf = true;
  }
  return ctf;
  }
  ///
  /// 获得池数组对应的网站数量
  ///
  static void WebNums()
  {
  List weblist = new List();
  //string[] poolns = pooln.Split(',');
  foreach (string i in pls)
  {
  if (hs.ToString() != &quot;&quot;)
  weblist.Add(hs.ToString().Split(',').Length.ToString());
  else
  weblist.Add(&quot;0&quot;);
  }
  nums = weblist.ToArray();
  

  }
  ///
  ///检测应用程序池的名称
  ///
  ///应用程序池的名称
  ///指定的限制数
  ///该池中网站的数量
  ///
  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() == &quot;true&quot;)
  ting += 1;
  if (ting >= pls.Length) break;
  for (int i = 0; i < pls.Length; i++)
  {
  if (boolhs[pls].ToString() == &quot;false&quot;)//如果哪个池的站点量可以容纳
  {
  int d = c - int.Parse(nums);
  

  if (si < c)
  {
  for (int j = 0; j < si; j++)
  if (chname(kt[j]))//判断名称是否存在
  movepool(kt[j], AppPoolName, pls);//转移站点
  }
  else
  {
  for (int j = 0; j < d; j++)
  if (chname(kt[j]))
  movepool(kt[j], AppPoolName, pls);
  }
  if (si-d < 0) break;
  si = si - d;
  

  boolhs[pls] = &quot;true&quot;;
  }
  }
  }
  //需要新建的情况
  if(si>0)
  {
  int sy = int.Parse(Math.Ceiling((double)si / (double)c).ToString());//新建多少个
  for (int j = 1; j 数组
  ///
  ///
  public static string[] GetApplicationPools()
  {
  DirectoryEntry directoryEntry = new DirectoryEntry(&quot;IIS://LOCALHOST/W3SVC/AppPools&quot;);
  if (directoryEntry == null) return null;
  List list = new List();
  foreach (DirectoryEntry entry2 in directoryEntry.Children)
  {
  PropertyCollection properties = entry2.Properties;
  list.Add(entry2.Name.ToString().Trim());
  }
  return list.ToArray();
  }
  

  ///
  /// 获得所有的应用程序池和对应站点
  ///
  static void getpoolweb()
  {
  DirectoryEntry root = null;
  try
  {
  root = new DirectoryEntry(&quot;IIS://localhost/W3SVC&quot;);
  }
  catch
  {
  return;
  }
  foreach (DirectoryEntry website in root.Children)
  {
  try
  {
  if (website.SchemaClassName != &quot;IIsWebServer&quot;) continue;
  

  string comment = website.Properties[&quot;ServerComment&quot;][0].ToString();
  DirectoryEntry siteVDir = website.Children.Find(&quot;Root&quot;, &quot;IISWebVirtualDir&quot;);
  string poolname = &quot;&quot;;
  try
  {
  poolname = siteVDir.Properties[&quot;AppPoolId&quot;][0].ToString().Trim();
  }
  catch (Exception ex)
  {
  Console.WriteLine(ex.Message);
  }
  

  if (poolname == &quot;&quot;)
  {
  try
  {
  poolname = website.Properties[&quot;AppPoolId&quot;][0].ToString().Trim();
  }
  catch (Exception ex)
  {
  Console.WriteLine(ex.Message);
  }
  }
  //if (pooln == &quot;&quot;) pooln = poolname;
  //else pooln += &quot;,&quot; + poolname;
  //string[] poolns = pooln.Split(',');
  foreach (string i in pls)
  {
  if (i == poolname)
  {
  if (hs.ToString() == &quot;&quot;)
  hs = comment;
  else hs += &quot;,&quot; + comment;
  }
  }
  }
  catch (Exception ex)
  {
  Console.WriteLine(ex.Message);
  }
  }
  

  root.Close();
  }
  

  ///
  /// 新建池
  ///
  /// 应用程序池名称
  ///
  public static DirectoryEntry AddAppPool(string AppPoolName)
  {
  try
  {
  DirectoryEntry appPool = new DirectoryEntry(&quot;IIS://localhost/W3SVC/AppPools&quot;);
  DirectoryEntry findPool = null;
  try
  {
  findPool = appPool.Children.Find(AppPoolName, &quot;IIsApplicationPool&quot;);
  }
  catch (Exception) { }
  if (findPool == null)
  {
  findPool = appPool.Children.Add(AppPoolName, &quot;IIsApplicationPool&quot;);
  findPool.CommitChanges();
  appPool.CommitChanges();
  }
  //pooln += &quot;,&quot; + AppPoolName;
  List a = new List();
  foreach (string b in pls)
  a.Add(b);
  a.Add(AppPoolName);
  pls = a.ToArray();//添加新池到数组中
  WebNums();
  boolhs.Add(AppPoolName, &quot;false&quot;);
  return findPool;
  }
  catch (Exception ex)
  {
  Console.WriteLine(ex.Message);
  return null;
  }
  }
  ///
  /// 移动网站到新池
  ///
  /// 网站名称
  /// 旧池名称
  /// 新池名称
  static void movepool(string webns,string poolold, string poolns)
  {
  try
  {
  DirectoryEntry root = new DirectoryEntry(&quot;IIS://localhost/W3SVC&quot;);
  foreach (DirectoryEntry website in root.Children)
  {
  if (website.SchemaClassName != &quot;IIsWebServer&quot;) continue;
  string comment = website.Properties[&quot;ServerComment&quot;][0].ToString();
  if (comment == webns)
  {
  DirectoryEntry siteVDir = website.Children.Find(&quot;Root&quot;, &quot;IISWebVirtualDir&quot;);
  siteVDir.Invoke(&quot;Put&quot;, new object[2] { &quot;AppPoolId&quot;, poolns });
  siteVDir.CommitChanges();
  website.Invoke(&quot;Put&quot;, new object[2] { &quot;AppPoolId&quot;, poolns });
  website.CommitChanges();
  }
  }
  for (int i = 0; i < pls.Length; i++)//遍历旧池并修改原数目数组的数据
  {
  if (pls == poolold)
  {
  nums = (int.Parse(nums) - 1).ToString();
  string[] h = hs[poolold].ToString().Split(',');
  string hnew = &quot;&quot;;
  foreach (string s in h)
  if (s != webns)
  {
  if (hnew == &quot;&quot;)
  hnew = s;
  else hnew += &quot;,&quot; + s;
  }
  hs[poolold] = hnew;
  if (hs[poolns].ToString() == &quot;&quot;) hs[poolns] = webns;
  else hs[poolns] += &quot;,&quot; + webns;
  }
  if (pls == poolns)
  {
  WebNums();
  nums = (int.Parse(nums) + 1).ToString();
  }
  }
  }
  catch (Exception ex)
  {
  

  Console.WriteLine(ex.Message);
  }
  }
  #endregion
  }
  



附件:http://down.运维网.com/data/2362946


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-649103-1-1.html 上篇帖子: change Windows user password through IIS web portal 下篇帖子: 在自己的电脑上架个网站!win7+IIS7+花生壳架设网站图文教程__iPc_me
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表