iis|網站
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());
}