I am trying to clean up over 150K of files within a directory using .NET 3.5 and PowerShell. Since there are so many files I do not want to impact the server performance by reading everything in at once. Is there any mechanism with .NET 3.5 or PowerShell or methods accessible via PInvoke that would allow me to lazily load the files? Thank you for your assistance.
Asked
Active
Viewed 3,205 times
1 Answers
3
If by "load" you mean to get a list of the files in a directory in a lazy manner, you have a couple of options.
As of .NET 4.0, you can use the new Directory.EnumerateFiles APIs to get a streaming (lazy) sequence of files in a particular directory. The search returns items on-demand, and so doesn't require as much memory as the existing GetFiles methods.
If you cannot use .NET 4, then you will have to roll your own streaming file enumerator. This would require using the FindFirstFile and FindNextFile Win32 APIs. However, you could take a look at this implementation on CodeProject, as it appears to be just that.
LBushkin
- 125,412
- 32
- 212
- 261
-
Thank you for your answer. We cannot use .NET 4.0 (yet) and I think if we really want to the second option would work just fine. – smaclell Feb 03 '11 at 17:23