C# directory.getfiles memory help

前端 未结 5 1894
梦毁少年i
梦毁少年i 2020-12-11 20:05

Here is the code I’m using:

using (StreamWriter output = new StreamWriter(Path.Combine(masterdestination, \"Master.txt\")))
{
     string masterfolders = sou         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 20:37

    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.

提交回复
热议问题