当前位置:文档之家› C#程序中操作IIS的应用程序池与站点分配

C#程序中操作IIS的应用程序池与站点分配

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

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

编程题目:用户输入一个数字或者一个数字+一个名字。

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

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

条件:一、把DefautlAppPool应用程序池或者含有字符"AppPool #"的应用程序池里面的超出的站点分配到AppPool #?应用程序池中("?"代表数字)二、如果aspnet1应用程序池里面的网站数超出用户限制的数字,则分配到新建应用程序池的命名方式为aspnet1-?;("?"代表数字,表示从属aspnet1下的分支)三、如二设置aspnet2,aspnet3,aspnet4应用程序池四、当网站名字和应用程序池的名字相同时,将不进行操作。

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

新建一个控制台应用程序(C#编程语言,使用vs2005版本制作)添加引用:System.DirectoryServicesclass 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());elseweblist.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(.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"); mitChanges();mitChanges();}//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 });mitChanges();website.Invoke("Put", new object[2] { "AppPoolId", poolns });mitChanges();}}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}更多信息请查看IT技术专栏。

相关主题