Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
7 Jun 2006 1  
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:

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:
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