我如何在Windows 8.1获得本地文件夹的大小(WinRT的) [英] How do I get Local folder size in Windows 8.1 (winRT)
问题描述
我有一个8.1的Windows Store应用。我试图找到本地文件夹Size.?(e.g。210 MB)
I have an Windows 8.1 Store App. I am trying to find the Local Folder Size.?(e.g. 210 MB)
我有这将下载一些内容到我的本地文件夹中的应用程序。我想检查我的本地文件夹的大小(如多少MB目前有)。
I have an application which would download some of content into my local folder. I want to check the size of my local folder (such as how many MB are currently there).
我试图寻找通过MSDN和谷歌,但一直没能。找到它的任何东西。
I have tried looking through MSDN and Google but haven't been able to find anything on it.
注:我有一个文件夹和子这样不仅文件,这是在本地文件夹..
Note : I have a folder and subfolder so not only files which is in local folder..
推荐答案
You are able to access the LocalFolder
via the ApplicationData.LocalFolder
property.
这 LocalFolder
是的 StorageFolder
对象。 StorageFolder
有一个名为的 GetBasicPropertiesAsync
。
This LocalFolder
is a StorageFolder
object. StorageFolder
has a method called GetBasicPropertiesAsync
.
GetBasicPropertiesAsync
返回 BasicProperties
对象。这 BasicProperties
对象具有的 尺寸
属性,它会告诉你该项目所涉及的大小(文件夹或文件)。我相信,尺寸
是字节( ULONG
)。
GetBasicPropertiesAsync
returns a BasicProperties
object. This BasicProperties
object has a Size
property which tells you the size of the item in question (folder or file). I believe that Size
is in bytes (a ulong
).
完整的命令可在单个行完成在异步
的方法是这样的:
The complete command can be done in a single line in an async
method like this:
(等待ApplicationData.LocalFolder.GetBasicPropertiesAsync())大小;
您也可以拆分的每一步,如果你需要的任何。其他信息
You can also split up each step if you need any other information.
编辑:很显然,这并不工作,以及希望。该解决方案是创建一个查询并总结了所有文件。你可以使用LINQ做到这一点。
Apparently this does not work as well as hoped. The solution is to create a query and sum up all of the files. You can do this using 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);
这篇关于我如何在Windows 8.1获得本地文件夹的大小(WinRT的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!