You are able to access the LocalFolder
via the ApplicationData.LocalFolder
property.
This LocalFolder
is a StorageFolder
object. StorageFolder
has a method called GetBasicPropertiesAsync
.
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:
(await ApplicationData.LocalFolder.GetBasicPropertiesAsync()).Size;
You can also split up each step if you need any other information.
Edit: 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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…