计算 Windows 文件夹大小的最快方法是什么?

问题描述 投票:0回答:10

我需要计算数百个文件夹的大小,有些是 10MB,有些可能是 10GB,我需要一种超快速的方法来使用 C# 获取每个文件夹的大小。

我的最终结果希望是:

文件夹1 10.5GB

文件夹2 230MB

文件夹3 1.2GB

...

c# .net filesystems
10个回答
38
投票

添加对 Microsoft Scripting Runtime 的引用并使用:

Scripting.FileSystemObject fso = new Scripting.FileSystemObject();
Scripting.Folder folder = fso.GetFolder([folder path]);
Int64 dirSize = (Int64)folder.Size;

如果您只需要大小,这比递归快很多


13
投票

好吧,这很糟糕,但是...

使用名为 dirsize.bat 的递归 dos 批处理文件:

@ECHO OFF
IF %1x==x GOTO start
IF %1x==DODIRx GOTO dodir
SET CURDIR=%1
FOR /F "usebackq delims=" %%A IN (`%0 DODIR`) DO SET ANSWER=%%A %CURDIR%
ECHO %ANSWER%
GOTO end
:start
FOR /D %%D IN (*.*) DO CALL %0 "%%D"
GOTO end
:dodir
DIR /S/-C %CURDIR% | FIND "File(s)"
GOTO end
:end

注意:第 5 行最后一个“%%A”后面应该有一个制表符,而不是空格。

这就是您要查找的数据。 它将相当快地处理数千个文件。 事实上,它在不到 2 秒的时间内就完成了我的整个硬盘驱动器的操作。

像这样执行文件

dirsize | sort /R /+25
以便看到首先列出的最大目录。

祝你好运。


1
投票

在 .Net 中没有简单的方法可以做到这一点;您将必须循环遍历每个文件和子目录。 请参阅示例此处了解其操作方法。


1
投票

如果您右键单击一个大目录,然后单击属性,您可以看到计算大小需要大量时间......我认为我们无法在这方面击败 MS。您可以做的一件事是索引目录/子目录的大小,如果您要一遍又一遍地计算它们......这将显着提高速度。

您可以使用类似的方法在 C# 中递归计算目录大小

static long DirSize(DirectoryInfo directory)
{
    long size = 0;

    FileInfo[] files = directory.GetFiles();
    foreach (FileInfo file in files)
    {
        size += file.Length;
    }

    DirectoryInfo[] dirs = directory.GetDirectories();

    foreach (DirectoryInfo dir in dirs)
    {
        size += DirSize(dir);
    }

    return size;
}

1
投票

你可以这样做,但是在获取文件夹大小时没有 fast=true 设置,你必须将文件大小相加。

    private static IDictionary<string, long> folderSizes;

    public static long GetDirectorySize(string dirName)
    {
        // use memoization to keep from doing unnecessary work
        if (folderSizes.ContainsKey(dirName))
        {
            return folderSizes[dirName];
        }

        string[] a = Directory.GetFiles(dirName, "*.*");

        long b = 0;
        foreach (string name in a)
        {
            FileInfo info = new FileInfo(name);
            b += info.Length;
        }

        // recurse on all the directories in current directory
        foreach (string d in Directory.GetDirectories(dirName))
        {
            b += GetDirectorySize(d);
        }

        folderSizes[dirName] = b;
        return b;
    }

    static void Main(string[] args)
    {
        folderSizes = new Dictionary<string, long>();
        GetDirectorySize(@"c:\StartingFolder");
        foreach (string key in folderSizes.Keys)
        {
            Console.WriteLine("dirName = " + key + " dirSize = " + folderSizes[key]);
        }

        // now folderSizes will contain a key for each directory (starting
        // at c:\StartingFolder and including all subdirectories), and
        // the dictionary value will be the folder size
    }

1
投票

Dot Net Pearls 有一个与这里描述的方法类似的方法。 令人惊讶的是, System.IO.DirectoryInfo 类没有执行此操作的方法,因为这似乎是一种常见的需求,并且无需在每个文件系统对象上进行本机/托管转换,执行此操作可能会更快。 我确实认为,如果速度是关键因素,请编写一个非托管对象来执行此计算,然后从托管代码中的每个目录调用一次。


1
投票

我能找到的 4.0-4.5 框架上计算文件大小及其在磁盘上的计数的最快方法是:

using System.IO;
using System.Threading;
using System.Threading.Tasks;

class FileCounter
{
  private readonly int _clusterSize;
  private long _filesCount;
  private long _size;
  private long _diskSize;

  public void Count(string rootPath)
  {
    // Enumerate files (without real execution of course)
    var filesEnumerated = new DirectoryInfo(rootPath)
                              .EnumerateFiles("*", SearchOption.AllDirectories);
    // Do in parallel
    Parallel.ForEach(filesEnumerated, GetFileSize);
  }

  /// <summary>
  /// Get real file size and add to total
  /// </summary>
  /// <param name="fileInfo">File information</param>
  private void GetFileSize(FileInfo fileInfo)
  {
    Interlocked.Increment(ref _filesCount);
    Interlocked.Add(ref _size, fileInfo.Length);
  }
}

var fcount = new FileCounter("F:\\temp");
fcount.Count();

对我来说,这种方法是我在 .net 平台上能找到的最好的方法。顺便说一句,如果您需要计算磁盘上的簇大小和实际大小,您可以执行下一步:

using System.Runtime.InteropServices;

private long WrapToClusterSize(long originalSize)
    {
        return ((originalSize + _clusterSize - 1) / _clusterSize) * _clusterSize;
    }

private static int GetClusterSize(string rootPath)
    {
        int sectorsPerCluster = 0, bytesPerSector = 0, numFreeClusters = 0, totalNumClusters = 0;
        if (!GetDiskFreeSpace(rootPath, ref sectorsPerCluster, ref bytesPerSector, ref numFreeClusters,
                              ref totalNumClusters))
        {
            // Satisfies rule CallGetLastErrorImmediatelyAfterPInvoke.
            // see http://msdn.microsoft.com/en-us/library/ms182199(v=vs.80).aspx
            var lastError = Marshal.GetLastWin32Error();
            throw new Exception(string.Format("Error code {0}", lastError));
        }
        return sectorsPerCluster * bytesPerSector;
    }
[DllImport(Kernel32DllImport, SetLastError = true)]
    private static extern bool GetDiskFreeSpace(
        string rootPath,
        ref int sectorsPerCluster,
        ref int bytesPerSector,
        ref int numFreeClusters,
        ref int totalNumClusters);

当然你需要在第一个代码部分重写 GetFileSize() :

private long _diskSize;
private void GetFileSize(FileInfo fileInfo)
    {
        Interlocked.Increment(ref _filesCount);
        Interlocked.Add(ref _size, fileInfo.Length);
        Interlocked.Add(ref _diskSize, WrapToClusterSize(fileInfo.Length));
    }


0
投票
我不知道它有多适合您的用例,但假设您的计算机使用

NTFS文件系统,绝对最快的方法可能是直接读取文件表,解析它并找到大小那样。 您可以首先阅读一份规范,例如 libfsntfs 提供的

here

,或者 this 提供的规范。 $FILE_NAME 属性提供文件大小。

    


-1
投票

using System.IO; long GetDirSize(string dir) { return new DirectoryInfo(dir) .GetFiles("", SearchOption.AllDirectories) .Sum(p => p.Length); }

	
© www.soinside.com 2019 - 2024. All rights reserved.