I got thousands of files with a specific file extension in thousands of sub folders. Now, what is the fastest way to search with a pattern? I tried the method DirectoryInfo.GetFiles(rootfolder)
(~8 minutes) and a recursive custom method (~5 minutes).
private void WalkDirectoryTree(DirectoryInfo dr, string searchname)
{
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
try
{
files = dr.GetFiles(searchname + ".*");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (files != null)
{
foreach (FileInfo fi in files)
{
allFiles.Add(fi);
}
subDirs = dr.GetDirectories();
foreach (DirectoryInfo di in subDirs)
{
WalkDirectoryTree(di, searchname);
}
}
}
Is there any faster way to do it?
allFiles
? Do you needFileInfo
or the filename ? \$\endgroup\$ – Heslacher Dec 19 '14 at 11:43