我有一个Windows 8.1商店应用程序。我试图找到本地文件夹大小。(例如210 MB)如何在Windows 8.1中获取本地文件夹大小(winRT)
我有一个应用程序将下载一些内容到我的本地文件夹。我想检查我的本地文件夹的大小(例如当前有多少MB)。
我已经试过通过MSDN和谷歌,但一直没能找到任何东西。
注:我有一个文件夹和子这样不仅文件,这是在本地文件夹..
我有一个Windows 8.1商店应用程序。我试图找到本地文件夹大小。(例如210 MB)如何在Windows 8.1中获取本地文件夹大小(winRT)
我有一个应用程序将下载一些内容到我的本地文件夹。我想检查我的本地文件夹的大小(例如当前有多少MB)。
我已经试过通过MSDN和谷歌,但一直没能找到任何东西。
注:我有一个文件夹和子这样不仅文件,这是在本地文件夹..
您可以通过ApplicationData.LocalFolder
属性来访问LocalFolder
。
This LocalFolder
is a StorageFolder
object。 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;
}
你能解释一下这段代码的工作原理吗? –
已更新,希望对 –
有帮助+1;好多了。 –