February 2008 - Posts

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

 

Posted by n.ocampo with 3 comment(s)
Filed under: ,