sohan vanani
sohan vanani

Reputation: 1574

Get total size of LocalFolder Windows Phone 8.1

In my app i am using LocalStorage. I have saved photos to localfolder. I want total used memory size of localfolder.

BasicProperties BP = await ApplicationData.Current.LocalFolder.GetBasicPropertiesAsync();
var size = BP.Size;
Debug.WriteLine("size = {0}", size);

I am using this code but every time I am getting

size = 0

I have looked at this Answer but i don't think its proper way to get total total size of localfolder i don't want to use for loop because there will be hundreds of file in my localfolder.

Upvotes: 0

Views: 259

Answers (1)

Mostafa Darwish
Mostafa Darwish

Reputation: 91

You have to walk through folders to calculate the size of files inside

public async System.Threading.Tasks.Task<long> GetFolderSize(Windows.Storage.StorageFolder folder)
{
    long size = 0;
    // For files
    foreach (Windows.Storage.StorageFile thisFile in await folder.GetFilesAsync())
    {
        Windows.Storage.FileProperties.BasicProperties props = await thisFile.GetBasicPropertiesAsync();
        size += props.Size;
    }
    // For folders
    foreach (Windows.Storage.StorageFolder thisFolder in await folder.GetFoldersAsync())
    {
       size += await GetFolderSize(thisFolder);
    }
    return size;
}

Upvotes: 1

Related Questions