0

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)

Mehdi Souregi
  • 3,153
  • 5
  • 36
  • 53

2 Answers2

1
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>

mybirthname
  • 17,949
  • 3
  • 31
  • 55
0

You can try something like this

foreach(var file in Directory.EnumerateFiles(path)) {
    //access your files
}
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78