[英]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.