Multiple Filters On Directory.GetFiles Method
If you tried to use Directory class located in System.IO namespace, you noticed GetFiles method that is used to retrieve names of the files of specified directory. But, unlike good practices in some other programming tools, Directory.GetFiles method can't return multiple extension (unless you use "*" filter that returns all files but you can get that without using filter at all).
There are few different solutions to select multiple file extensions with GetFiles method. Here is mine, I wrote a function that takes same parameters like GetFiles method but its filter supports multiple file filters:
[ C# ]
/// <summary>
/// Returns file names from given folder that comply to given filters
/// </summary>
/// <param name="SourceFolder">Folder with files to retrieve</param>
/// <param name="Filter">Multiple file filters separated by | character</param>
/// <param name="searchOption">File.IO.SearchOption,
/// could be AllDirectories or TopDirectoryOnly</param>
/// <returns>Array of FileInfo objects that presents collection of file names that
/// meet given filter</returns>
public string[] getFiles(string SourceFolder, string Filter,
System.IO.SearchOption searchOption)
{
// ArrayList will hold all file names
ArrayList alFiles = new ArrayList();
// Create an array of filter string
string[] MultipleFilters = Filter.Split('|');
// for each filter find mathing file names
foreach (string FileFilter in MultipleFilters)
{
// add found file names to array list
alFiles.AddRange(Directory.GetFiles(SourceFolder, FileFilter, searchOption));
}
// returns string array of relevant file names
return (string[])alFiles.ToArray(typeof(string));
}
[ VB.NET ]
''' <summary>
''' Returns file names from given folder that comply to given filters
''' </summary>
''' <param name="SourceFolder">Folder with files to retrieve</param>
''' <param name="Filter">Multiple file filters separated by | character</param>
''' <param name="searchOption">File.IO.SearchOption,
''' could be AllDirectories or TopDirectoryOnly</param>
''' <returns>Array of FileInfo objects that presents collection of file names that
''' meet given filter</returns>
Public Function getFiles(ByVal SourceFolder As String, ByVal Filter As String, _
ByVal searchOption As System.IO.SearchOption) As String()
' ArrayList will hold all file names
Dim alFiles As ArrayList = New ArrayList()
' Create an array of filter string
Dim MultipleFilters() As String = Filter.Split("|")
' for each filter find mathing file names
For Each FileFilter As String In MultipleFilters
' add found file names to array list
alFiles.AddRange(Directory.GetFiles(SourceFolder, FileFilter, searchOption))
Next
' returns string array of relevant file names
Return alFiles.ToArray(Type.GetType("System.String"))
End Function
You can copy this function to your application. This example call getFiles function and lists selected file names including sub directories:
[ C# ]
protected void btnListSomeFiles_Click(object sender, EventArgs e){
// Request files with gif, jpg, png, bmp, aspx and vb extension
// Extensions are separated with | character
string[] sFiles = getFiles(Server.MapPath("~/"),
"*.gif|*.jpg|*.png|*.bmp|*.aspx|*.vb",
SearchOption.AllDirectories);
// Write returned file names on page
foreach (string FileName in sFiles)
{
Response.Write(FileName + "<br />");
}
}
[ VB.NET ]
Protected Sub btnListSomeFiles_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnListSomeFiles.Click
' Request files with gif, jpg, png, bmp, aspx and vb extension
' Extensions are separated with | character
Dim sFiles() As String = getFiles(Server.MapPath("~/"), _
"*.gif|*.jpg|*.png|*.bmp|*.aspx|*.vb", _
SearchOption.AllDirectories)
' Write returned file names on page
For Each FileName As String In sFiles
Response.Write(FileName + "<br />")
Next
End Sub
Getting file names with multiple filters is very common task and some newer version of .Net Framework will probably have Directory.getFiles method that supports multiple filters. Until then you can use this function and get quite satisfactory results. Happy coding!
Related articles:
1. Mainpulating Files and Directories in ASP.NET