Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can find out the available free space in a universal Windows Phone 8.1 App

does anyone know how to access the available free space in a universal Windows Phone 8.1 App? In a Windows Phone 8(.1) Silverlight App I could use this Code:

int availableStorage = IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace;

But System.IO.IsolatedStorage isn't available in a Windows (Phone) 8.1 App.

like image 541
Matt126 Avatar asked May 03 '14 14:05

Matt126


1 Answers

It may be done like in answers to this question. As I've tried, the method like in the code below returns the number of free bytes:

public async Task<UInt64> GetFreeSpace()
{
    StorageFolder local = ApplicationData.Current.LocalFolder;
    var retrivedProperties = await local.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace" });
    return (UInt64)retrivedProperties["System.FreeSpace"];
}

// usage:
UInt64 myFreeSpace = await GetFreeSpace();

More about porperties to retrive (their format etc.) you can find at MSDN.


Some more information - note that method gets free space of a folder it's reffering to. So if we run it like this:

public async Task<UInt64> GetFreeSpace(StorageFolder folder)
{
    var retrivedProperties = await folder.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace" });
    return (UInt64)retrivedProperties["System.FreeSpace"];
}

// and use it like this:
UInt64 spaceOfInstallationFolder = await GetFreeSpace(ApplicationData.Current.LocalFolder);
UInt64 spaceOfMusicLibrary = await GetFreeSpace(KnownFolders.MusicLibrary);

We will get results dependant on Settings of the User's Phone:

  • ApplicationData.Current.LocalFolder reffers to a place where the App was installed - so if User had set that Apps will be installed on SD, then you will see free place on SD card,
  • KnownFolders.MusicLibrary (for example, it can be PicturesLibrary and so on, also ensure that you added cappabilities in your manifest file, otherwise you will get an exception) - the same situation dependand on User settings - it can be space on Phone or SD

So if the App is installed on Phone, then reffering to LocalFolder you will get space on Phone. If you want space on SD then you can for example run method like this (remember about capabilities):

UInt64 spaceOfMusicLibrary = await GetFreeSpace((await KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault());

Note also that getting free space of the Phone in case User had set everything to be installed on SD (Apps, Music, Pictures) is useless as you won't be able to use it (unauthorized access). Simply - if you have an access to a folder you can get its available space.

like image 84
Romasz Avatar answered Oct 08 '22 19:10

Romasz