System.IO.Directory.GetFiles() does not support multiple search pattern

I have a task that will delete files in a certain directory, specifically a file with “*.csv” and *.log” extensions. Unfortunately System.IO.Directory.GetFiles(string,string) doesn't support multiple search pattern.

To be able to achieve my requirement I created a method that accepts an array of strings as parameter to hold the extensions.


public void DeleteFiles(List<string> extensions)
{
    foreach (string extension in extensions)
    {
        foreach(string file in System.IO.Directory.GetFiles(@"C:\MyFolder",extension))
        {               
           File.Delete(file);                           
        }
    }
}

I wish that on the next release of the framework it would have another overload for System.IO.Directory.GetFiles()

something like System.IO.Directory.GetFiles(string,string[]) or System.IO.Directory.GetFiles(string, List<string>)

 

UPDATE: Check out Keith's approach on searching a directory for files using multiple search patterns and using it in with Extension Methods

 

Published 02-01-2008 12:20 AM by n.ocampo
Filed under: ,

Comments

# How To: Search a directory for files using multiple search patterns

Friday, February 01, 2008 5:02 PM by Keith Rull

I was browsing at DevPinoy.org today when I saw this interesting post by n@rds about searching for files

# re: System.IO.Directory.GetFiles() does not support multiple search pattern

Friday, February 01, 2008 5:05 PM by keithrull

Hi n@rds,

here's my approach.

www.keithrull.com/.../HowToSearchADirectoryForFilesUsingMultipleSearchPatterns.aspx

Thanks,

Keith

# Another Real-World Example On When To Use Extension Methods

Monday, February 04, 2008 10:33 AM by Keith Rull

It&#39;s funny, just after posting this post in response to an article that N@rds posted that I realized