Monday, September 7, 2009

Get files with multiple extension from a directory

Backgroud:

System.IO.Directory class provide a method GetFiles(...) to get a list of files from a directory. But, there is a limitation if programmers want to code to get a list of files with multiple extension provided.

Solution:

In order to get this done, I come up with a simple solution to create a method with 3 parametters.

Method:

public static string[] getFilesWithMultipleExtensions(string path, string extensionList, string delimeter)
{
System.Collections.Generic.List strFiles = new System.Collections.Generic.List();

foreach (string strExt in extensionList.Split(delimeter.ToCharArray()))
{
strFiles.AddRange(Directory.GetFiles(path, strExt));
}
return strFiles.ToArray();
}




Usage:
getFilesWithMultipleExtensions("c:\\path",".jpg;.jpeg;.jpe;.jfif;.tiff;.tif;.bmp;.dib;.png",";");

No comments: