Here is the code I’m using:
using (StreamWriter output = new StreamWriter(Path.Combine(masterdestination, \"Master.txt\")))
{
string masterfolders = sou
As mentioned in the answer here if using .NET 4.0, you can use the static EnumerateFiles method on the Directory class to get an IEnumerable instead of an string[], which is leading to all the memory consumption.
If you are working with a version of .NET before .NET 4.0, you can easily mimic this functionality by calling the FindFirstFileEx, FindNextFile, etc, etc, methods through the P/Invoke layer.
Then, for every file that is returned from a call to FindFirstFile/FindNextFile you would yield return the item.
This will cut down on the memory consumption as EnumerateFiles would for directories with large numbers of files because you aren't loading them all into an array up front, but yielding them for processing as you find them.