I'm writing a C# application with .NET 4 to read and process data files (*.dat) from a list of directories.
Currently, I'm checking if a directory contains files using the following function:
private bool FilesPresent()
{
string[] DIRS = Directory.GetDirectories(dataFileDirectoryPath, "*.*", SearchOption.TopDirectoryOnly);
foreach (string d in DIRS)
{
string[] FILES = Directory.GetFiles(d, "*.*", SearchOption.AllDirectories);
if (FILES.Length > 0) { return true; }
}
return false;
}
I've also tried some alternative solutions from the following post: How to quickly check if folder is empty (.NET)?
It's important to note that some of the directories have in excess of 1,000,000 files. Even reading the million+ file names into a string[] is taking a long time.
How can I implement this differently so that it runs faster?
Putting it simply; I'd just like to know the fastest way of checking if the directory is not empty. I'm not concerned with retrieving the filenames at this time.