Guys I am trying to remove a specific IP from IIS site bindings through C# (Microsoft Web Administration).
Basically what I want to do is, is to check all site's binding in IIS and if any binding IP matches user-provided IP, it should remove it and if the particular site doesn't have any other bindings with different IP then it should stop the site.
However, I can't seem to remove the IP in a loop as I am getting "Collection was modified; enumeration operation may not execute." error.
This is my code :
static void Main(string[] args)
{
ServerManager mgr = new ServerManager();
foreach (Site s in mgr.Sites)
{
Console.WriteLine("Site {0}", s.Name);
foreach (Application app in s.Applications)
{
Console.WriteLine("\tApplication: {0}", app.Path);
foreach (VirtualDirectory virtDir in app.VirtualDirectories)
{
Console.WriteLine("\t\tVirtual Dir: {0}", virtDir.Path);
}
}
BindingCollection bindingCollection = s.Bindings;
foreach (Binding b in bindingCollection)
{
Console.WriteLine("\n Bindings: {0}",b.EndPoint);
if (b.EndPoint != null)
{
//Hard Coded IP for testing purpose
if (b.EndPoint.ToString() == "1.1.1.1:86")
{
Console.WriteLine("Removing this ip : {0}",b.EndPoint.ToString());
bindingCollection.Remove(b);
}
}
}
mgr.CommitChanges();
}
Console.ReadLine();
}
What should I do to remove the binding ? I need to remove it in a loop because a site may have more than 1 binding which uses the same IP.