2011-01-13 9 views
0

サーバー2003で実行されているWebサイトの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

私はそのショットを出します。 –

+0

あなたは先生、天才です!ありがとう! –

関連する問題