【问题标题】:How do I get Local folder size in Windows 8.1 (winRT)如何在 Windows 8.1 (winRT) 中获取本地文件夹大小
【发布时间】:2014-04-10 13:19:51
【问题描述】:

我有一个 Windows 8.1 应用商店应用程序。我正在尝试查找本地文件夹大小。?(例如 210 MB)

我有一个应用程序可以将一些内容下载到我的本地文件夹中。我想检查我的本地文件夹的大小(例如当前有多少 MB)。

我曾尝试浏览 MSDN 和 Google,但无法找到任何内容。

注意:我有一个文件夹和子文件夹,所以不仅是本地文件夹中的文件..

【问题讨论】:

    标签: c# windows-runtime windows-store-apps winrt-xaml windows-8.1


    【解决方案1】:

    您可以通过ApplicationData.LocalFolder 属性访问LocalFolder

    这个LocalFolder 是一个StorageFolder 对象。 StorageFolder 有一个名为 GetBasicPropertiesAsync 的方法。

    GetBasicPropertiesAsync 返回一个BasicProperties 对象。这个BasicProperties 对象有一个Size 属性,它告诉您有问题的项目(文件夹或文件)的大小。我相信Size 以字节为单位(ulong)。

    完整的命令可以在 async 方法中的一行中完成,如下所示:

    (await ApplicationData.LocalFolder.GetBasicPropertiesAsync()).Size;

    如果您需要任何其他信息,也可以拆分每个步骤。

    编辑:显然这并不像希望的那样有效。解决方案是创建一个查询并汇总所有文件。您可以使用 Linq 执行此操作。

    using System.Linq;
    
    // Query all files in the folder. Make sure to add the CommonFileQuery
    // So that it goes through all sub-folders as well
    var folders = ApplicationData.LocalFolder.CreateFileQuery(CommonFileQuery.OrderByName);
    
    // Await the query, then for each file create a new Task which gets the size
    var fileSizeTasks = (await folders.GetFilesAsync()).Select(async file => (await file.GetBasicPropertiesAsync()).Size);
    
    // Wait for all of these tasks to complete. WhenAll thankfully returns each result
    // as a whole list
    var sizes = await Task.WhenAll(fileSizeTasks);
    
    // Sum all of them up. You have to convert it to a long because Sum does not accept ulong.
    var folderSize = sizes.Sum(l => (long) l);
    

    【讨论】:

    • 已更新,希望对您有所帮助
    • 这没有给我尺寸,但它只会显示 0(零)
    • 这似乎行不通,但还是有道理的。大小对于大型文件夹来说并不是一件容易的事,因此能够快速获取基本属性 - 它不应该在基本属性调用中返回。我尝试了一些扩展属性,但这些似乎也不起作用,因此您最好自己通过迭代文件夹中的所有文件来计算它。
    • 我认为 Filip 可能是对的。不过,使用 Linq 很容易做到这一点。编辑以包含解决方案。
    • 这对我来说非常有效,而且速度很快
    【解决方案2】:

    C# 解决方案适用于中小型文件夹,但如果您的应用程序(无论出于何种原因)包含大量文件,此方法将花费大量时间,甚至可能会耗尽内存。我遇到了这种情况并选择编写一个获取文件夹大小的 c++ winrt 组件,我可以证明它运行得更快且内存更少。该函数的代码如下,在c#代码中,您可以使用LocalState文件夹的Path属性调用GetAppUsedSpace。

        #include "pch.h"
        #include "NativeFileHelper.h"
    
        #include <string>
        #include <vector>
        #include <stack>
        #include <iostream>
        #include <windows.h>
    
        using namespace Platform;
    
        NativeFileHelper::NativeFileHelper()
        {
        }
    
    
        unsigned __int64 ListFiles(std::wstring path, std::wstring mask) {
            HANDLE hFind = INVALID_HANDLE_VALUE;
            WIN32_FIND_DATA ffd;
            std::wstring spec;
            std::stack<std::wstring> directories;
    
            directories.push(path);
    
            unsigned __int64 result = 0;
    
            while (!directories.empty()) {
                path = directories.top();
                spec = path + L"\\" + mask;
                directories.pop();
    
                hFind = FindFirstFileEx(spec.c_str(), FindExInfoStandard, &ffd, FINDEX_SEARCH_OPS::FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH);
                if (hFind == INVALID_HANDLE_VALUE)  {
                    return result;
                }
    
                do {
                    if (wcscmp(ffd.cFileName, L".") != 0 &&
                        wcscmp(ffd.cFileName, L"..") != 0) {
                        if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
                            directories.push(path + L"\\" + ffd.cFileName);
                        }
                        else {
                            //This is file name
                            result += ffd.nFileSizeLow + (ffd.nFileSizeHigh * MAXDWORD);
                            //files.push_back(path + "\\" + ffd.cFileName);
                        }
                    }
                } while (FindNextFile(hFind, &ffd) != 0);
    
                if (GetLastError() != ERROR_NO_MORE_FILES) {
                    FindClose(hFind);
                    return result;
                }
    
                FindClose(hFind);
                hFind = INVALID_HANDLE_VALUE;
            }
    
            return result;
        }
    
        unsigned __int64 NativeFileHelper::GetAppUsedSpace(Platform::String^ pathOfFolder)
        {
            unsigned __int64 size = ListFiles(pathOfFolder->Data(), L"*");
    
            return size;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 2012-09-30
      • 2015-03-03
      相关资源
      最近更新 更多