Souregi Souregi - 11 days ago 8
C# Question

Getting file by file located in a folder

I got a folder that contains millions and millions of files (under windows server2012), and, using Directory.GetFiles needs a lot of times to get all these files,

I am just wondering if there is any c# method that can return file by file instead of getting all these files at once,

(just like an

SQLDataReader
and using
while (reader.Read())
to get line by line within DB)

Answer
DirectoryInfo di = new DirectoryInfo(yourPath);

foreach (var fi in di.EnumerateFiles())
{
     Console.WriteLine(fi.Name);
}

You can use DirectoryInfo.EnumerateFiles

Returns an enumerable collection of file information in the current directory.

Return Value Type: System.Collections.Generic.IEnumerable<FileInfo>