Reputation: 77
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();
}
Upvotes: 1
Views: 6552
Reputation: 45101
You should switch to Directory.EnumerateFiles()
cause it is lazy and doesn't need to built up the complete list in the first place.
Upvotes: 2
Reputation: 921
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.
Upvotes: 0
Reputation: 1219
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
Upvotes: 0