locked
How to get folder size in Windows 8?

    Question

  • Hello,

    Currently I am using this code to get folder size, but every time it returns 0, there is multiple files with sizes in KB in my folder.
    Is there anything wrong with the code?

    BasicProperties properties = await ApplicationData.Current.LocalFolder.GetBasicPropertiesAsync();
    var x = properties.Size.ToString();
    Thanks in advance.
     
    • Edited by zee_patel Friday, June 27, 2014 11:38 AM
    Friday, June 27, 2014 11:38 AM

Answers

  • It always returns 0 for folders. You have to walk through folder to calc it size

    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;
    }

    • Marked as answer by zee_patel Friday, June 27, 2014 1:52 PM
    Friday, June 27, 2014 11:53 AM