简体   繁体   中英

Best way to get path all files, subfolder in large directory?

Below is my code to get all files in large directory (Framework 3.5). It run ~ 1 hour, but cannot finish, memory in Task Manager upto 1,600,000K

void ApplyAllFiles(DirectoryInfo dir)
{
    String space = "          ";
    foreach (FileInfo f in dir.GetFiles())
    {
        try
        {            
            int size = Convert.ToInt32(f.Length / 1024);
            if (size > filesize && isFileType(f.FullName) == true)
            {
                pw.WriteLine(f.LastWriteTime + space + size + space + f.FullName);
            }
        }
        catch
        { }
    }
    foreach (DirectoryInfo d in dir.GetDirectories())
    {
        try
        {
            ApplyAllFiles(d);
        }
        catch
        {

        }
    }
}

If I use Java code, just 5 min for finish and memory always < 100,000 K. I think C# code maybe has problem (getFiles & getDirectories is not good for large Directory). I hope someone can help.


I SOLVED IT BY TAKE FOREACH LOOP INSIDE TRY { }.

You can create a non-recursive extension method:

public static IEnumerable<FileInfo> GetFilesDepthFirst(this DirectoryInfo root,
    string dirPattern = "*", string filePattern = "*.*")
{
    var stack = new Stack<DirectoryInfo>();
    stack.Push(root);
    while (stack.Count > 0)
    {
        var current = stack.Pop();
        IEnumerable<FileInfo> files = Enumerable.Empty<FileInfo>();
        IEnumerable<DirectoryInfo> dirs = Enumerable.Empty<DirectoryInfo>();

        try
        {
#if NET35
            dirs = current.GetDirectories(searchPattern: dirPattern);
            files = current.GetFiles(searchPattern: filePattern);
#else
            dirs = current.EnumerateDirectories(searchPattern: dirPattern);
            files = current.EnumerateFiles(searchPattern: filePattern);                  
#endif
        }
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }

        foreach (FileInfo file in files)
            yield return file;

        foreach (DirectoryInfo dir in dirs)
            stack.Push(dir);
    }
}

Which you can call like:

var dInfo = new DirectoryInfo("C:\\");
var allFiles = dInfo.GetFilesDepthFirst(filePattern: "*.txt");
foreach(FileInfo file in allFiles)
{
    // build output string
}

As other users have stated, Directory.GetFiles is terrible. Directory.EnumerateFiles is an alternative .

Reference from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM