title: Close all open forms except the main menu in C#
body: <p>Trying to close all forms except for the main menu using </p>
  
  <pre><code>FormCollection formsList = Application.OpenForms;
  </code></pre>
  
  <p>with a foreach loop and saying, </p>
  
  <pre><code>if (thisForm.Name != "Menu") thisForm.Close();
  </code></pre>
  
  <p>Which works ok, it skips the menu, and closes the first, but it then errors: </p>
  
  <blockquote>
    <p>Collection was modified; enumeration operation may not execute</p>
  </blockquote>
  
  <p>and stops. I have tried a few places, and they all say that this foreach loop is the way to do it, and it is especially annoying as I am not updating my forms list after closing the forms, which I thought might work. The only thing I could think of was to start at the back and work forward using a while.</p>