什么是计算在.NET中的一个目录的大小,最好的方法是什么?最好的、大小、目录、方法

2023-09-02 10:24:20 作者:猫街小巷浅时光

我已经写了下面的程序通过目录手动遍历并计算在C#/它的大小NET:


受保护的静态浮动CalculateFolderSize(字符串的文件夹)
{
    浮folderSize = 0.0;
    尝试
    {
        //检查该路径是否有效
        如果(!Directory.Exists(文件夹))
            返回folderSize;
        其他
        {
            尝试
            {
                的foreach(在Directory.GetFiles字符串的文件(文件夹))
                {
                    如果(File.Exists(文件))
                    {
                        FileInfo的finfo =新的FileInfo(文件);
                        folderSize + = finfo.Length;
                    }
                }

                的foreach(在Directory.GetDirectories字符串目录(文件夹))
                    folderSize + = CalculateFolderSize(DIR);
            }
            赶上(NotSupportedException异常E)
            {
                Console.WriteLine(无法计算文件夹大小:{0},e.Message);
            }
        }
    }
    赶上(UnauthorizedAccessException E)
    {
        Console.WriteLine(无法计算文件夹大小:{0},e.Message);
    }
    返回folderSize;
}

我有反复运行这个程序进行了大量的文件夹中的应用程序。我不知道是否有计算与.NET中的文件夹的大小更有效的方法?我没有看到具体的框架,任何东西。我应该使用P / Invoke和Win32 API的?什么是计算一个文件夹的大小在.NET中的最有效的方法是什么?

解决方案

我不相信这是一个Win32 API来计算一个目录中占用的空间,但我站在这个予以纠正。如果有,那么我会认为浏览器会使用它。如果你在资源管理器中的大目录的属性,所花费的时间给你的文件夹的大小是成正比的文件/子目录包含的数量。

您日常似乎相当整齐和放大器;简单。请记住,你正在计算的文件长度的总和,而不是消耗在盘上的实际空间。空间在簇的结束消耗浪费的空间,文件流等被忽略了。DotNet并行计算的使用误区 二

I've written the following routine to manually traverse through a directory and calculate its size in C#/.NET:


protected static float CalculateFolderSize(string folder)
{
    float folderSize = 0.0f;
    try
    {
        //Checks if the path is valid or not
        if (!Directory.Exists(folder))
            return folderSize;
        else
        {
            try
            {
                foreach (string file in Directory.GetFiles(folder))
                {
                    if (File.Exists(file))
                    {
                        FileInfo finfo = new FileInfo(file);
                        folderSize += finfo.Length;
                    }
                }

                foreach (string dir in Directory.GetDirectories(folder))
                    folderSize += CalculateFolderSize(dir);
            }
            catch (NotSupportedException e)
            {
                Console.WriteLine("Unable to calculate folder size: {0}", e.Message);
            }
        }
    }
    catch (UnauthorizedAccessException e)
    {
        Console.WriteLine("Unable to calculate folder size: {0}", e.Message);
    }
    return folderSize;
}

I have an application which is running this routine repeatedly for a large number of folders. I'm wondering if there's a more efficient way to calculate the size of a folder with .NET? I didn't see anything specific in the framework. Should I be using P/Invoke and a Win32 API? What's the most efficient way of calculating the size of a folder in .NET?

解决方案

I do not believe there is a Win32 API to calculate the space consumed by a directory, although I stand to be corrected on this. If there were then I would assume Explorer would use it. If you get the Properties of a large directory in Explorer, the time it takes to give you the folder size is proportional to the number of files/sub-directories it contains.

Your routine seems fairly neat & simple. Bear in mind that you are calculating the sum of the file lengths, not the actual space consumed on the disk. Space consumed by wasted space at the end of clusters, file streams etc, are being ignored.

 
精彩推荐
图片推荐