Click here to Skip to main content
15,886,199 members
Articles / Web Development / ASP.NET
Article

Using DirectoryInfo class to search for files in a directory on both single pattern and multiple patterns

Rate me:
Please Sign up or sign in to vote.
3.30/5 (8 votes)
7 Jun 20062 min read 105.6K   1.2K   24   12
The articles simplifies searching files in a directory with single search pattern and multiple search patterns.

Introduction

In this article, I have tried to simply understanding of using DirectoryInfo class in .NET to search for files in a specific directory. The DirectoryInfo class provides information about a given directory which can be set through the constructor while creating the DirectoryInfo object. Once we get a DirectoryInfo object bound to a directory on the file system, we can get any sort of information about it including files it contains. I have included examples of searching for specific files in the directory instead of getting the complete list. The .NET framework provides a complete search mechanism for searching filenames and file extensions. For a very simple example:

C#
DirectoryInfo dinfo = new DirectoryInfo(completeDirPath);
/* get files from the directory as a files collection */
FileInfo[] finfo = dinfo.GetFiles(strExtension);
Here, I create a DirectoryInfo object bound to a directory given by completeDirPath and then get a list of all files in that directory which have the extension of strExtension. This extension can be any e.g. *.txt and *.gif and *.doc etc. The GetFiles(strExtension) method returns a list of type FileInfo where each FileInfo represents a file and provides detailed information about that file. Here is a limitation! The GetFiles(strExtension) method can take only one pattern as argument. This means that we can search either *.txt files or *.doc files at a time. If we want to get a list of all txt and doc files, then we will have to use a little logic to get the required lists and join them by ourselves. This happens sometime when we really need to do it. For instance, I need to get all JPEG images in my directory which can be either jpg or jpeg. Both are valid formats but the search pattern will not allow me to search for them. The solution to the problem is quite simple:
C#
public override FileInfo[] getFiles(string ext)
{
/* Pointer to our directory */
if (dirPath == null)
    return null;

DirectoryInfo dinfo = new DirectoryInfo(dirPath);
string[] exts = ext.Split(',');
FileInfo[][] finfo = new FileInfo[exts.Length][];
int i = 0;

foreach(string e in exts)
{
    /* get files from the directory as a files collection */
    finfo[i++] = dinfo.GetFiles(e);
}

int tlength = 0;

for (i = 0 ; i < finfo.Length ; i++)
{
    tlength += finfo[i].Length;
}

FileInfo[] res = new FileInfo[tlength];
int j = 0;
int rindex = 0;

for (i = 0 ; i < finfo.Length ; i++)
{
    for (j = 0 ; j < finfo[i].Length ; j++)
    {
        res[rindex++] = finfo[i][j];
    }
}

return res;
}
This method receives a comma seperated list of extensions and searches the directory for each extension one by one. It makes a jagged array for containing lists against each file extension and them merges those arrays to make a single complete files list. I have attached a complete running example with this article. In the example, if you search more than one extensions, the second method is called and if you search for only one extension, the simple straight forward method is called. I have created an abstract base class DirectoryInfoEx and two classes SingleExt and MultipleExt inherit from it. This article is part of my .NET surfing and I soon dispatch more detailed and advanced topics based on this example. Keep reading and happy dotnetting :-)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Australia Australia
C/C++, C# .NET, VB.NET, Linux, Oracle, PostgreSQL, MYSQL

Comments and Discussions

 
Generalthe same using LINQ Pin
Kubin23-Aug-10 2:49
Kubin23-Aug-10 2:49 
GeneralCool Pin
dsovino10-Oct-07 9:48
dsovino10-Oct-07 9:48 
QuestionWill it work fine for *.* ??? Pin
nitinr70822-Aug-07 20:07
nitinr70822-Aug-07 20:07 
AnswerRe: Will it work fine for *.* ??? Pin
nitinr70822-Aug-07 20:28
nitinr70822-Aug-07 20:28 
SuggestionRe: Will it work fine for *.* ??? Pin
phil.o2-Jul-11 7:32
professionalphil.o2-Jul-11 7:32 
GeneralSuggestion Pin
Alex Cater29-Nov-06 5:33
Alex Cater29-Nov-06 5:33 
GeneralRe: Suggestion Pin
M Gillespie20-Apr-07 0:07
M Gillespie20-Apr-07 0:07 
GeneralRe: Suggestion [modified] Pin
Alex Cater20-Apr-07 12:26
Alex Cater20-Apr-07 12:26 
GeneralRe: Suggestion Pin
HosamAly17-Jan-11 5:45
HosamAly17-Jan-11 5:45 
QuestionsearchPattern "*.txt" is actually "*.txt*"????? Pin
sheir24-Jul-06 5:47
sheir24-Jul-06 5:47 
AnswerRe: searchPattern "*.txt" is actually "*.txt*"????? Pin
icebear_X16-Jun-11 7:42
icebear_X16-Jun-11 7:42 
GeneralHi dear Pin
lakshithap@dsisamson.com18-Jun-06 18:13
lakshithap@dsisamson.com18-Jun-06 18:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.