C#列表所有IIS網站以及相關網站屬性

來源:互聯網
上載者:User
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());
    
  
   } 




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.