This blog defines how to list
all files matching search pattern in directory and subdirectories.
///
<summary>
///
Lists all matching search pattern filenames and filesize in directory
///
</summary>
///
<param name="rootDirPath">root
directory path like C:\test\</param>
///
<param name="searchPattern">file
search pattern like *.js for searching all javascript files</param>
private static
void ListAllFilesInDirectoryAndSubdirectories(string
rootDirPath, string searchPattern)
{
var dir = new
DirectoryInfo(rootDirPath);
if (!dir.Exists)
{
Console.WriteLine("Directory
doesn't exists: " + dir.ToString());
return;
}
DirectoryInfo
sourceDir = new DirectoryInfo(rootDirPath);
FileInfo[] files
= sourceDir.GetFiles(searchPattern);
//Get only files which you need to work with.
foreach (var
myFile in files)
{
try
{
Console.WriteLine("Filename:"
+ myFile.FullName);
Console.WriteLine("Filesize:"
+ myFile.Length);
}
catch (Exception
ex)
{
Console.WriteLine(ex.Message);
}
}
DirectoryInfo[]
subDirectories = sourceDir.GetDirectories();
//Get subdirectories
foreach (var
subDirectory in subDirectories)
{
ListAllFilesInDirectoryAndSubdirectories(subDirectory.FullName, searchPattern);
}
}