I've read discussions about difference between Directory.EnumerateFiles and Directory.GetFiles().
I understand that internally they both use System.IO.FileSystemEnumerableFactory.CreateFileNameIterator()
The difference is that EnumerateFiles might use deferred execution (lazy), while GetFiles() does a ToArray, so the function is already executed.
But what happens if files and folders are added to the dictionary during the iteration. Will the iteration only iterate over the items that were present during the EnumerateFiles()?
Even worse: what happens if files are removed during iterations: will they still be iterated?
Thanks Michal Komorowski. However when trying his solution myself I saw a remarkable distinction between Directory.EnumerateFiles and Directory.GetFiles():
This will give different output.
Results:
So the difference in usage between EnumerateFiles and GetFiles is more than just performance.
So if you expect that your folder changes while enumerating carefully choose the desired function
There is only one way to check:
Initially C:\Temp contains 3 files: a.txt, b.txt and c.txt. During the iteration one of these file is being deleted and one is being created. Finally, the C:\Temp contains the following files: a.txt, b.txt and d.txt However, in the console you will see the original content of this directory i.e.: