Files and Storage on Windows Phone 8

This article explains storage in Windows Phone, it will be a local data storage and we will see various methods to use it. Let's get a quick overview of how the data is stored locally in a Windows Phone.

Files-and-Storage-on-Windows-Phone-1.jpg

There are two locations in which the data is stored:

  1. Installation Folder: This is the place from where the application gets all its items for the phone, it is also referred to as the App Data folder. This is a ready-only folder where we can just read the files that are required for installation and we cannot perform any writing operations.
  2. Local Folder: It is formerly known as Isolated Storage, here we can perform both read and write operations, this is the place we are going to spend most of our time.

There are various methods for addressing storage location:

File Type / API
 
Installation Folder
 
Local Folder
 
Example
 
Local Database Data Context appdata:/ isostore:/ MyDataContext db =new MyDataContext("isostore:/mydb.sdf"); 
File access using WP7.1 Isolated Storage API Not supported StorageFile and StorageFolder APIs var isf =IsolatedStorageFile.
GetUserStoreForApplication();
File access using Windows.Storage APIs via URIs ms-appx:/// ms-appdata:///local/ StorageFile storageFile = await Windows.Storage.StorageFile.
GetFileFromApplicationUriAsync
(new Uri("ms-appdata:///local/
CaptainsLog.store"));
File access using Windows.Storage APIs via StorageFolder Refrence Windows.Application
Model.Package.
Current.
InstalledLocation
Windows.Storage.
ApplicationData.
Current.LocalFolder
var localFolder = Windows.Storage.ApplicationData.
Current.LocalFolder;
Windows.Storage.StorageFile
storageFile = await localFolder.GetFileAsync
("CaptainsLog.store");

Windows Phone 8 File Access Alternatives

Let's have a look at getting a reference of a file by three different ways:

  1. WP7.1 Isolated Storage APIs

    var isf = IsolatedStorageFile.GetUserStoreForApplication();IsolatedStorageFileStream fs = new IsolatedStorageFileStream("CaptainsLog.Store", FileMode.Open, isf);
     
  2. WP8 Storage APIs using URI

    StorageFile storageFile = await 
    Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/CaptainsLog.store"));
     
  3. WP8 Storage APIs

    Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;Windows.Storage.StorageFile storageFile =localFolder.GetFileAsync("CaptainsLog.store");

So, now we are aware of various types of file handing techniques, in future articles we will learn how to use them individually.


Similar Articles