How to retrieve Free Space details in WP programmatically?


How many of you want to check the available free space in your Windows Phone device? It is very simple to get the details from the settings page. But think about a case where you want to check the free space from your app and want to chose some logic to notify the user.

 

In this small but useful post, we will discuss about the API and will share a small piece of code, which will allow you to know the available free space in user’s phone device.

 

The sealed class named “IsolatedStorageFile” available under the namespace “System.IO.IsolatedStorage” provides you a static method “GetUserStoreForApplication()” which allows you to obtain user-scoped isolated storage for use by an application. The IsolatedStorageFile instance has a property called “AvailableFreeSpace” which returns a long value representing the amount of free space available in the phone storage. The unit of the returned value is bytes. From this value, you can easily calculate the free space in Kilo Bytes (KB), Mega Bytes (MB) and/or Giga Bytes (GB).

 

Here is the complete code that you might want to reference to use in your application:

 

using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
    var sizeInBytes = isolatedStorageFile.AvailableFreeSpace;
    var sizeInKB = sizeInBytes / 1024.0;
    var sizeInMB = sizeInKB / 1024.0;
    var sizeInGB = sizeInMB / 1024.0;
    
    MessageBox.Show(string.Format("Available Free Space: {0} GB", Math.Round(sizeInGB, 2)));
}

 

Here you can see that, first we are getting the instance of user’s isolated store by calling the static method of the sealed class named IsolatedStorageFile.GetUserStoreForApplication() and the calling the AvailableFreeSpace property to get the free space information.

 

Here is what you will see after calculating the byte unit to GB :

 

How to retrieve Free Space details in Windows Phone?

 

I hope, this simple tip will help you to retrieve the available free storage space in your application/game and then ask the user to take some steps based on your business requirement.

 

Thanks a lot for visiting my blog. Subscribe to my blog’s RSS feed or newsletter to get the article updates directly in your inbox. I am also available in Twitter and Facebook. Connect with me to get regular updates on technical posts. Happy coding!!!

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

2 comments

  1. Thanks kunal for ur valuable tips & guides..😊

    ReplyDelete
  2. The above code snippet worked fine for windows Phone 8.0,But it is not working in WP 8.1 XAML Universal Apps. in 8.1 IsolatedStorageFile is changed to StorageFile and there is no method like GetUserStoreForApplication(). PLease Help me how to perform this in Windows Phone 8.1?

    ReplyDelete

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top