using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.DirectoryServices;
using System.Reflection;
using System.Text.RegularExpressions;
int TotalServerCount=0;
DirectoryEntry rootfolder = new DirectoryEntry("IIS://localhost/W3SVC");
//TotalServerCount=rootfolder.Children.SchemaFilter.Count;
foreach (DirectoryEntry child in rootfolder.Children)
{
if (child.ScheMaclassName == "IISWebServer")
{
TotalServerCount+=1;
}
}
//循環獲取所有站點詳細屬性寫入數組中
string [] arrayServerID = new string[TotalServerCount];//站點標識符
string [] arrayServerIP = new string[TotalServerCount];//站點主機頭
string [] arrayServerPort = new string[TotalServerCount];//站點主機頭
string [] arrayServerHeader = new string[TotalServerCount];//站點主機頭
string [] arrayServerPath = new string[TotalServerCount];//站點主機頭
string [] arrayServerComment = new string[TotalServerCount];//站點主機頭
string [] arrayServerBinds = new string[TotalServerCount];//站點主機頭
string currentServerBindings;//綁定主機頭IP端口字符串
char[] a=":".ToCharArray();
string [] currentBingdings =new string[2];
int i=0;
foreach (DirectoryEntry child in rootfolder.Children)
{
if (child.ScheMaclassName == "IISWebServer")
{
arrayServerID.SetValue(child.Name.ToString(),i);
arrayServerComment.SetValue(child.PropertIEs["ServerComment"].Value.ToString(),i);
currentServerBindings=child.PropertIEs["ServerBindings"].Value.ToString();
currentBingdings=currentServerBindings.Split(a);
arrayServerIP.SetValue(currentBingdings[0],i);
arrayServerPort.SetValue(currentBingdings[1],i);
arrayServerHeader.SetValue(currentBingdings[2],i);
foreach (DirectoryEntry rootChild in child.Children)
{
if((rootChild.ScheMaclassName == "IISWebVirtualDir")&&(rootChild.Name.ToString()=="root"))
{
if(rootChild.PropertIEs["Path"].Value==null)
{
arrayServerPath.SetValue("",i);
}
else
{
arrayServerPath.SetValue(rootChild.PropertIEs["Path"].Value.ToString(),i);
}
}
}
i+=1;
}
}
//寫入到datagrid中去
//循環從數組中讀取數據
for(i=0;i<TotalServerCount;i++)
{
listVIEw1.Items.Add((i+1).ToString());
listVIEw1.Items[i].SubItems.Add(arrayServerID.GetValue(i).ToString());
listVIEw1.Items[i].SubItems.Add(arrayServerComment.GetValue(i).ToString());
listVIEw1.Items[i].SubItems.Add(arrayServerIP.GetValue(i).ToString());
listVIEw1.Items[i].SubItems.Add(arrayServerPort.GetValue(i).ToString());
listVIEw1.Items[i].SubItems.Add(arrayServerHeader.GetValue(i).ToString());
listVIEw1.Items[i].SubItems.Add(arrayServerPath.GetValue(i).ToString());
}