Improve the performance for enumerating files and folders using .NET

后端 未结 7 1412
一生所求
一生所求 2020-12-08 07:51

I have a base directory that contains several thousand folders. Inside of these folders there can be between 1 and 20 subfolders that contains between 1 and 10 files. I\'d

相关标签:
7条回答
  • 2020-12-08 08:48

    You are using a Linq. It would be faster if you wrote your own method for searching Directories recursively with you're special case.

    public static DateTime retval = DateTime.Now.AddDays(-60);
    
    public static void WalkDirectoryTree(System.IO.DirectoryInfo root)
    {
        System.IO.FileInfo[] files = null;
        System.IO.DirectoryInfo[] subDirs = null;
    
        // First, process all the files directly under this folder 
        try
        {
            files = root.GetFiles("*.*");
        }
        // This is thrown if even one of the files requires permissions greater 
        // than the application provides. 
        catch (UnauthorizedAccessException e)
        {
            // This code just writes out the message and continues to recurse. 
            // You may decide to do something different here. For example, you 
            // can try to elevate your privileges and access the file again.
            log.Add(e.Message);
        }
        catch (System.IO.DirectoryNotFoundException e)
        {
            Console.WriteLine(e.Message);
        }
    
        if (files != null)
        {
            foreach (System.IO.FileInfo fi in files)
            {
              if (fi.LastWriteTime < retval)
              {
                oldFiles.Add(files[i]);
              }
    
                Console.WriteLine(fi.FullName);
            }
    
            // Now find all the subdirectories under this directory.
            subDirs = root.GetDirectories();
    
            foreach (System.IO.DirectoryInfo dirInfo in subDirs)
            {
                // Resursive call for each subdirectory.
                WalkDirectoryTree(dirInfo);
            }
        }            
    }
    
    0 讨论(0)
提交回复
热议问题