You should switch to Directory.EnumerateFiles()
cause it is lazy and doesn't need to built up the complete list in the first place.
Efficient use of 'Directory.GetFiles' with 'StartsWith', 'Contains' and 'EndsWith' to filter file names
-
18-03-2022 - |
Question
Is there a more efficient way to filter file names using Directory.GetFiles and 'StartsWith', 'Contains' and 'EndsWith' with rather than the way I am currently doing it?
_files = Directory.GetFiles(_path);
if (!String.IsNullOrEmpty(_startsWith))
{
_files = _files.Where(x => x.StartsWith(_startsWith)).ToArray();
}
if (!String.IsNullOrEmpty(_contains))
{
_files = _files.Where(x => x.Contains(_contains)).ToArray();
}
if (!String.IsNullOrEmpty(_endsWith))
{
_files = _files.Where(x => x.EndsWith(_endsWith)).ToArray();
}
Solution
OTHER TIPS
I think following overloads will help you:
Directory.GetFiles(strPath, "*" + strFilter, SearchOption.TopDirectoryOnly); // to get behaviour of EndsWith
Directory.GetFiles(strPath, "*" + strFilter + "*", SearchOption.TopDirectoryOnly); // to get behaviour of Contains
Directory.GetFiles(strPath, strFilter + "*", SearchOption.TopDirectoryOnly); // to get behaviour of StartsWith
If you are trying to cram everything into a single line of Linq, you can do something like the following:
_files = _files.Where(_ => !String.IsNullOrEmpty(_startsWith)).Where(x => x.StartsWith(_startsWith)).ToArray();
However, as mentioned above, the Directory.GetFiles will almost certainly be the slowest part of the code here and you are better off sticking with something which is easier to read.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow