2011-01-13 20 views
0

我試圖以編程方式更改在Server 2003上運行的網站的IP。 當我運行以下代碼,站點綁定本身得到改變,但還有其他主機頭值需要更改。我如何改變這些?如何使用DirectoryEntry(「IIS:// Localhost/W3SVC」)更改具有多個主機標頭條目的IP地址

protected static void ChangeServerIP(string old_ip, string new_ip) 
    { 
     int siteChangedCount = 0; 
     DirectoryEntry entry = new DirectoryEntry("IIS://LocalHost/W3SVC"); 

     if (entry != null) 
     { 
      foreach (DirectoryEntry site in entry.Children) 
      { 
       if (site.SchemaClassName == "IIsWebServer") 
       { 
        Console.WriteLine("Server Binding: " + site.Properties["ServerBindings"][0].ToString()); 

        if (site.Properties["ServerBindings"][0].ToString().Contains(old_ip)) 
        { 
         string ServerComment = site.Properties["ServerComment"].Value.ToString(); 

         Console.WriteLine("Changing " + ServerComment + "'s IP address from " + old_ip + " to " + new_ip); 

         site.Properties["ServerBindings"].Value = site.Properties["ServerBindings"][0].ToString().Replace(old_ip, new_ip); 

         site.CommitChanges(); 
         siteChangedCount++; 
         Console.WriteLine("New IP address bound to site: " + ServerComment + " IP: " + site.Properties["ServerBindings"].Value.ToString()); 
        } 
       } 
      } 
     } 


     } 

回答

1

您是否嘗試將值分配給PropertyValueCollection中的第一項?

site.Properties["ServerBindings"][0].Value = site.Properties["ServerBindings"][0].ToString().Replace(old_ip, new_ip); 
+0

我會給你一個鏡頭。 – 2011-01-14 13:32:04

相關問題