Need to search the directory/sub-directories to find a file, would prefer it to stop once it has found one.
Is this a feature built into DirectoryInfo.GetFiles that I am missing, or should I be using something else (self-implemented recursive search)?
Have you tried DirectoryInfo.GetFiles([Your Pattern], SearchOption.AllDirectories).First();
Use
DirectoryInfo.EnumerateFiles()
instead which is lazily returning the files (as opposed toGetFiles
which is bringing the full file list into memory first) - you can addFirstOrDefault()
to achieve what you want:From MSDN:
(
DirectoryInfo.EnumerateFiles
requires .NET 4.0)The best method to use for pre-.NET 4.0, use FindFirstFile()
Requires this struct