c#讀取IIS中的網站相關屬性代碼

來源:互聯網
上載者:User
iis|網站

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   string currentServerComment=comboBox1.SelectedItem.ToString();
   string currentSiteNum = GetWebSiteNum(currentServerComment);
   string rootPath = "IIS://localhost/w3svc";
   string currentSitePath =rootPath+"/"+currentSiteNum;   
   DirectoryEntry siteEntry = new DirectoryEntry(currentSitePath); 

   string currentServerBindings=siteEntry.Properties["ServerBindings"].Value.ToString();
   char[] a=":".ToCharArray();
   string [] currentBingdings = null;   
   currentBingdings=currentServerBindings.Split(a);
   string currentServerIP=currentBingdings[0];
   string currentServerPort=currentBingdings[1];
   string currentServerHeader=currentBingdings[2];
   string currentServerHost="";
   string currentServerPath="";

   foreach (DirectoryEntry child in siteEntry.Children)
   {
    
    if((child.SchemaClassName == "IIsWebVirtualDir")&&(child.Name=="root"))
    {
     currentServerPath = child.Properties["Path"].Value.ToString();
     
    }
   }
 

   textBox2.Text=currentServerIP;
   textBox3.Text=currentServerPort;
   textBox4.Text=currentServerPath;
   textBox5.Text=currentServerHeader;
   textBox6.Text=currentServerHost;
  }

/// <summary>
  /// 根據網站名稱擷取網站標識符
  /// </summary> 
  
  public string GetWebSiteNum(string siteName)
  {

   Regex regex = new Regex(siteName);
   string tmpStr;
   string entPath = "IIS://localhost/w3svc";
   DirectoryEntry ent =new DirectoryEntry(entPath); 

   foreach(DirectoryEntry child in ent.Children)

   {
    if(child.SchemaClassName == "IIsWebServer")
    {
     if(child.Properties["ServerBindings"].Value != null)
     {
      tmpStr = child.Properties["ServerBindings"].Value.ToString();
      if(regex.Match(tmpStr).Success)
      {
       return child.Name;
      }
     }

     if(child.Properties["ServerComment"].Value != null)

     {
      tmpStr = child.Properties["ServerComment"].Value.ToString();
      if(regex.Match(tmpStr).Success)
      {
       return child.Name;
      }
     }
    }
   }
   return "";
   
  }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.