C#directory.getfiles内存的帮助 [英] C# directory.getfiles memory help

查看:461
本文介绍了C#directory.getfiles内存的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我使用的代码:

using (StreamWriter output = new StreamWriter(Path.Combine(masterdestination, "Master.txt")))
{
     string masterfolders = sourcefolder1;
     string[] filess = Directory.GetFiles(masterfolders, "*.txt");
     foreach (string file in filess)
     {
        output.WriteLine(Path.GetFileName(file));
     }
}  

这个代码将在用户的所有文件中搜索指定目录中的任何txt文件。这些目录有时含有200万的文件。

This code will search for all files in a user specified directory for any txt file. These directories sometimes contain 2million files.

监测这一过程,而它的运行我已经看到了它爬上800MB内存的使用情况。有没有一种方法可以让我保持这种过程的速度,并限制其占用的内存?或者它读取和转储并继续吗?哈希表?任何想法的将是真棒。

monitoring this process while it's running I’ve seen it climb up to 800MB memory usage. Is there a way I can preserve the speed of this process and limit the memory it uses? Or have it read and dump and continue? Hashtable? Any idea's would be awesome.

推荐答案

Directory.GetFiles真的很烂。如果您可以使用.NET 4.0,你应该考虑使用 Directory.EnumerateFiles 的。从文档:

Directory.GetFiles really sucks. If you can use .NET 4.0 you should look into using Directory.EnumerateFiles. From the docs:

EnumerateFiles和GetFiles的
方法的区别如下:当你
使用EnumerateFiles,你可以启动
全收集之前列举的名字
的集合
返还;当您使用的GetFiles,你
必须等待的名字
整个阵列要返回,然后才能访问$ B $数组b。因此,当你
有许多文件和
目录工作,EnumerateFiles可以
更有效。

The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

这篇关于C#directory.getfiles内存的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆