中文字幕免费精品_亚洲视频自拍_亚洲综合国产激情另类一区_色综合咪咪久久

C#程序中操作IIS的應用程序池與站點分配
來源:易賢網 閱讀:998 次 日期:2014-08-20 15:23:34
溫馨提示:易賢網小編為您整理了“C#程序中操作IIS的應用程序池與站點分配”,方便廣大網友查閱!

一個應用程序池可以有多個站點,一個站點只對應一個應用程序池。

編程由來:

存放在一個應用程序池里的站點過多就不便于操作,所以需把其中一些站點分配到其他程序池中。

編程題目:

用戶輸入一個數字或者一個數字+一個名字。程序對站點的所在應用程序池進行統計,用戶輸入的數字用于限制應用程序池里面的最大容量數,如果超出該容量,將把超出的站點分配到其他程序應用池,或者新建的一個應用程序池,把站點分配進去。

如果用戶輸入一個數字的情況,將遍歷所有程序應用池;如果用戶輸入一個數字+一個名字的情況,將只對該名字的應用程序池進行操作;如果站點的名字和應用程序池的名字一樣,將不進行操作。

條件:

一、把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

}

更多信息請查看IT技術專欄

更多信息請查看網絡編程
由于各方面情況的不斷調整與變化,易賢網提供的所有考試信息和咨詢回復僅供參考,敬請考生以權威部門公布的正式信息和咨詢為準!

2026國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
云南網警備案專用圖標
聯系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網
云南網警報警專用圖標
主站蜘蛛池模板: 历史| 康乐县| 巴马| 梅河口市| 钟山县| 安仁县| 太湖县| 遂川县| 额尔古纳市| 达尔| 辰溪县| 乃东县| 汶川县| 银川市| 都江堰市| 六安市| 宽城| 安达市| 邓州市| 资阳市| 商丘市| 阳东县| 鹤山市| 营口市| 工布江达县| 沙洋县| 芦山县| 屯留县| 那坡县| 汪清县| 永新县| 大新县| 桐乡市| 九龙城区| 遂平县| 中江县| 丹寨县| 广安市| 张家界市| 泾源县| 无棣县|