C#列表所有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());
    
  
   } 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疾风铸境

提供工作中碰到的和研究过的技术

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值