簡體   English   中英

如何在Windows 8.1(winRT)中獲取本地文件夾大小

[英]How do I get Local folder size in Windows 8.1 (winRT)

我有一個Windows 8.1 Store App。 我正在嘗試查找本地文件夾大小。(例如210 MB)

我有一個應用程序,可以將某些內容下載到本地文件夾中。 我想檢查本地文件夾的大小(例如當前有多少MB)。

我曾嘗試通過MSDN和Google進行搜索,但無法找到任何內容。

注意:我有一個文件夾和子文件夾,所以不僅有本地文件夾中的文件。

您可以通過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);

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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM