title: C# delete files in folder with filter
body: <p>I am using SharpDevelop to write a C# program (not console).
  I want to delete files within a specified directory, but be able to EXCLUDE files beginning, ending, or containing certain words.</p>
  
  <p>TO completely delete ALL files in a folder I am using this : </p>
  
  <pre><code>private void clearFolder(string FolderName)
  {
      DirectoryInfo dir = new DirectoryInfo(FolderName);
  
      foreach(FileInfo fi in dir.GetFiles())
      {
          fi.Delete();
      }
  
      foreach (DirectoryInfo di in dir.GetDirectories())
      {
          clearFolder(di.FullName);
          di.Delete();
      }
  }
  </code></pre>
  
  <p>I use it like </p>
  
  <pre><code>ClearFolder("NameOfFolderIWantToEmpty");
  </code></pre>
  
  <p>Is there a way to modify this so that I can delete all files and direcotries EXCEPT those files and directories containing specific words?</p>
  
  <p>Something like : </p>
  
  <pre><code>CleanFolder(FolderToEmpty,ExcludeAllFileAndDirectoriesContaingThisPhrase);
  </code></pre>
  
  <p>so that if I did</p>
  
  <pre><code>CleanFolder("MyTestFolder","_blink");
  </code></pre>
  
  <p>It would NOT delete files and directories with names like </p>
  
  <pre><code>_blinkOne (file)
  
  Test_blineGreen (file)
  
  Test_blink5 (directory)
  
  _blinkTwo (file within the Text_blink5 directory)
  
  Folder_blink (empty directory)
  </code></pre>
  
  <p>but WOULD delete files and directories like</p>
  
  <pre><code>test (file)
  
  test2 (directory)
  
  test3_file (file within test2 directory)
  
  test4 (empty directory)
  </code></pre>
  
  <p>I suspect I might have to iterate through each file and directory, checking them one at a time for the matching filter and deleting it if it does not match, but I am not sure how to do that.</p>
  
  <p>Something with <code>FileInfo()</code> and <code>DirectoryInfo()</code> perhaps?</p>
  
  <p>Can somebody help by providing a working example?
  (modified version of the above is preferred, but if a new method is required, as long as it doesn't require an outside dll, is OK.</p>