程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#列表所有IIS站點以及相關站點屬性

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

編輯:關於C語言

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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved