Nearly micro-optimization, however, you can improve readability and you could add exception handling.
You should also use EnumerateFiles
(if possible) which does not need to load all into memory before it starts filtering. Also, use Equals
with StringComparison.OrdinalIgnoreCase
instead of ToLower
since it's more efficient and less error-prone(the Turkey test).
public IEnumerable<FileInfo> FindFilesInDirectory(string dirPath, string searchName)
{
if (Directory.Exists(dirPath))
{
var dir = new DirectoryInfo(dirPath);
return dir.EnumerateFiles("*.*", SearchOption.AllDirectories)
.Where(fi => fi.Name.Equals(searchName, StringComparison.OrdinalIgnoreCase));
}
else
throw new ArgumentException("Directory doesn't exist.", dirPath);
}