title: Change IP of IIS7 Bindings in C#
body: <p>I have an IIS7 web server that runs loads of sites each with multiple bindings. Each site uses host headers AND their own IP.</p>
  
  <p>Using C# I need to change the IP on all bindings to 0.0.0.0 so they listen on all the IPs.</p>
  
  <p>My current code doesn't add the new bindings but clears the old ones. Any ides what I'm doing wrong?</p>
  
  <p>My Code:</p>
  
  <pre><code>using (ServerManager iisServerManager = new ServerManager())
          {
              foreach (Site site in iisServerManager.Sites)
              {
                  List&lt;Microsoft.Web.Administration.Binding&gt; bindings = new List&lt;Microsoft.Web.Administration.Binding&gt;();
  
                  foreach (Microsoft.Web.Administration.Binding binding in site.Bindings)
                  {
                      String protocol = binding.Protocol;
  
                      String hostHeader = binding.Host;
  
                      String ipAddress = "0.0.0.0";
  
                      Int32 port = binding.EndPoint.Port;
  
                      String bind = ipAddress + ":" + port + ":" + hostHeader;
  
                      binding.BindingInformation = bind;
  
                      bindings.Add(binding);
                  }
  
                  //Clear existing site bindings
                  site.Bindings.Clear();
  
                  Int32 bindingCount = site.Bindings.Count();
  
                  foreach (Microsoft.Web.Administration.Binding binding in bindings)
                  {
                      site.Bindings.Add(binding);
  
                      bindingCount = site.Bindings.Count();
                  }
  
                  bindingCount = site.Bindings.Count();
  
                  iisServerManager.CommitChanges();
              }
          }
  </code></pre>