5

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Matt126
  • 997
  • 11
  • 25

1 Answers1

9

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.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • If the app was installed to SD card, is this code work? – sunjinbo May 05 '14 at 06:06
  • @sunjinbo Yes - as I've just checked it will show free space on Phone/SD, according to where the App was installed. – Romasz May 05 '14 at 06:12
  • Thanks, and one more question, if my app was installed to SD by user, but I want to know the phone's free space, is there have a way? – sunjinbo May 05 '14 at 06:21
  • @sunjinbo I've added some information in answer's edit. Look that method reffers to StorageFolder so you get space dependant on a plave where the folder exists. – Romasz May 05 '14 at 07:57
  • @Romasz `UInt64 spaceOfMusicLibrary = await GetFreeSpace(KnownFolders.MusicLibrary);` gives an exception. Can you edit your answer?? – Vibhesh Kaul Jun 14 '16 at 07:12
  • @psyLogic I don't get what should I correct in answer. What exception you get, have you declared capabilities, what OS are you using? – Romasz Jun 14 '16 at 07:59
  • @Romasz its a NullReferenceException, if you simply do `var retrivedProperties = await (KnownFolders.MusicLibrary).Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace" }); return (UInt64)retrivedProperties["System.FreeSpace"];` – Vibhesh Kaul Jun 14 '16 at 12:33
  • here `KnownFolders.MusicLibrary` is just a reference that is not been instantiated. Have you even tried doing it??? – Vibhesh Kaul Jun 14 '16 at 12:34
  • @psyLogic In almost every answer I try out my code. The code from the above one works. *KnownFolders.MusicLibrary* is a virtual location - are you using suitable namespaces and/or assembly references? – Romasz Jun 14 '16 at 19:50
  • Oh this question is about Windows phone, my bad. But yes I'm using the suitable namespaces and assembly references and it works fine but only in the case of `KnownFolders.MusicLibrary` I'm getting an exception. I'm pretty sure that this code throws an exception on a Windows 10 Desktop. – Vibhesh Kaul Jun 15 '16 at 06:44
  • @psyLogic I'm not sure about desktop now, as I've tested it should work on W10 mobile. MusicLibrary is a virtual location and maybe on desktop it consists of much more phisical locations, therefore getting its free space may be hard. – Romasz Jun 15 '16 at 06:49
  • @Romasz I figured there's a workaround, so I created a folder inside music library an accessed it using `StorageFolder folder = await KnownFolders.MusicLibrary.GetFolderAsync("folderName"); var retrivedProperties = await folder.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace" }); return (UInt64)retrivedProperties["System.FreeSpace"];` and it worked! – Vibhesh Kaul Jun 15 '16 at 07:02
  • @psyLogic I wonder what free space you get in that case - does it correspond to the drive on which the folder exists? – Romasz Jun 15 '16 at 07:09
  • @Romasz yes, exactly! which is the same as music library when it has nothing but that particular folder in it. – Vibhesh Kaul Jun 15 '16 at 07:14
  • @psyLogic Thanks for info. Generally *MusicLibrary* exists on multiple drives, therefore that may be a problem if you try to get its free space in app. – Romasz Jun 15 '16 at 07:21
  • @Romasz In my case I'm building a voice recorder and the recording gets stored in a seperate folder in the Music library so I need to check whether there's space available in the folder before recording to it. So am I going in the right direction? – Vibhesh Kaul Jun 15 '16 at 08:12
  • @psyLogic Why not to allow the user to choose a folder, then check if there is a free space? – Romasz Jun 15 '16 at 10:21
  • @Romasz I cant, the requirements may not allow me to do that. – Vibhesh Kaul Jun 16 '16 at 14:17