Windows Phone

From Isolated Storage to LocalFolder – Getting Folder Size

If you want to determine the space used for a particular folder in Isolated Storage you have to work recursively through the files in the folder (and sub-folders) and determine their sizes. In the Windows API the StorageFolder gives the indication that you can get the BasicProperties for a folder and use this to get its size. Unfortunately this just returns zero for folders. Therefore you have to use the same basic approach but modified for the new API (and async code). Below is an example of a method to do that. Simply pass in the StorageFolder of interest and it will return the size e.g.

ulong size = await GetFolderSize( await Windows.Storage.ApplicationData.Current.LocalFolder.GetFolderAsync(“cheese”));

The method looks like this:-

public async System.Threading.Tasks.Task<ulong> GetFolderSize(Windows.Storage.StorageFolder folder)
    ulong size = 0;

    foreach (Windows.Storage.StorageFolder thisFolder in await folder.GetFoldersAsync())
       size += await GetFolderSize(thisFolder);

    foreach (Windows.Storage.StorageFile thisFile in await folder.GetFilesAsync())
       Windows.Storage.FileProperties.BasicProperties props = await thisFile.GetBasicPropertiesAsync();
       size += props.Size;

    return size;

You can use this across both Windows Phone 8 and Windows Store projects.

By Peter Foot

Microsoft Windows Development MVP