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);
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)
{
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)
{
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 :-)