ASP.NET: 快速获取文件大小和所有文件数量的方法?

ASP.NET: Fast way to get file size and count of all files?

下面代码有什么修改,来自:

https://www.devcurry.com/2010/07/calculate-size-of-folderdirectory-using.html

是否还需要 return 文件夹和子文件夹中所有文件的数量?

是否可以同时完成以节省时间?

C#

using System;
using System.Linq;
using System.IO;

namespace ConsoleApplication3
{
  class Program
  {
    static void Main(string[] args)
    {
      DirectoryInfo dInfo = new DirectoryInfo(@"C:/Articles");
      // set bool parameter to false if you
      // do not want to include subdirectories.
      long sizeOfDir = DirectorySize(dInfo, true);

      Console.WriteLine("Directory size in Bytes : " +
      "{0:N0} Bytes", sizeOfDir);
      Console.WriteLine("Directory size in KB : " +
      "{0:N2} KB", ((double)sizeOfDir) / 1024);
      Console.WriteLine("Directory size in MB : " + 
      "{0:N2} MB", ((double)sizeOfDir) / (1024 * 1024));

      Console.ReadLine();
    }

    static long DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
    {
       // Enumerate all the files
       long totalSize = dInfo.EnumerateFiles()
                    .Sum(file => file.Length);

       // If Subdirectories are to be included
       if (includeSubDir)
       {
          // Enumerate all sub-directories
          totalSize += dInfo.EnumerateDirectories()
                   .Sum(dir => DirectorySize(dir, true));
       }
       return totalSize;
    }
  }
}

VB

Imports System
Imports System.Linq
Imports System.IO

Module Module1

Sub Main()
   Dim dInfo As New DirectoryInfo("C:/Articles")
   ' set bool parameter to false if you
   ' do not want to include subdirectories.
   Dim sizeOfDir As Long = DirectorySize(dInfo, True)

   Console.WriteLine("Directory size in Bytes : " & _
    "{0:N0} Bytes", sizeOfDir)
   Console.WriteLine("Directory size in KB : " & _
    "{0:N2} KB", (CDbl(sizeOfDir)) / 1024)
   Console.WriteLine("Directory size in MB : " & _
    "{0:N2} MB", (CDbl(sizeOfDir)) / (1024 * 1024))

   Console.ReadLine()
End Sub

Private Function DirectorySize(ByVal dInfo As DirectoryInfo, _
   ByVal includeSubDir As Boolean) As Long
   ' Enumerate all the files
   Dim totalSize As Long = dInfo.EnumerateFiles() _
     .Sum(Function(file) file.Length)

   ' If Subdirectories are to be included
   If includeSubDir Then
     ' Enumerate all sub-directories
     totalSize += dInfo.EnumerateDirectories() _
      .Sum(Function(dir) DirectorySize(dir, True))
   End If
   Return totalSize
End Function

End Module

我还假设它可以在线程中执行,并将结果存储在数据库中以防需要很长时间。

更新 1:

来自 File count from a folder 我看到一个获取文件计数的答案(尽管不是被接受的答案)使用 LINQ,就像上面的代码一样:

var fileCount = (from file in Directory.EnumerateFiles(@"H:\iPod_Control\Music", "*.mp3", SearchOption.AllDirectories)
                        select file).Count();

我对 LINQ 没有太多经验,所以想知道代码是否应该或可以组合,以便 LINQ return 有两个值(大小和计数)以节省时间?我可以为每个任务设置一个单独的线程,但我认为如果每个文件夹只需要一个通道,效率会更高。

我们希望它能够处理数百万个子文件夹,每个子文件夹中都有一个 .png 和一个 .jpg 文件。

我们的文件夹看起来像:

 /images/3/4/2/4/7/6/5/4/3/4/3/2/abc-234345674243.png

尝试转储递归,并尝试转储 linq - 它很慢并且占用大量内存。

试试这个:

  Dim strFolder = "C:\Users\AlbertKallal\Desktop"

    Dim MyDir As New DirectoryInfo(strFolder)
    Dim MyFiles() As FileInfo = MyDir.GetFiles("*.*", SearchOption.AllDirectories)



    For Each MyFile As FileInfo In MyFiles
        tSize += MyFile.Length
    Next