Windows Phone Silverlight developers may have had a difficult experience implementing a good local storage model in their apps. Having to open and write to files in Isolated Storage wasn't one of the easiest tasks to do but thankfully, this model was improved in Windows Store development with the introduction of the StorageFile and StorageFolder and with universal app development, you have these to leverage in your apps.
Choosing the folder
Saving files into local storage is basically a matter of saving a file into a folder somewhere on disk. Your app will already expose the root folders that you can read and write to through ApplicationData.Current.
These folders are:
- ApplicationData.Current.LocalFolder;
- ApplicationData.Current.RoamingFolder;
- ApplicationData.Current.TemporaryFolder;
You can also create your own folders underneath these root folders that you can use to store and retrieve certain pieces of data. To do this, you get the root folder you want like so:
- var folder = ApplicationData.Current.LocalFolder;
- var folder = ApplicationData.Current.LocalFolder;
- var newFolder = await folder.CreateFolderAsync("NewFolder", CreationCollisionOption.OpenIfExists);
Saving Text
Saving text into your storage folder is a very straight forward task. FileIO is a class that provides helper methods for reading and writing to StorageFiles. One of these methods is WriteTextAsync that takes your StorageFile and string of text as parameters.
You can easily implement this by creating a new StorageFile with the folder you've created as follows:
- var textFile = await newFolder.CreateFileAsync("text.txt");
- await FileIO.WriteTextAsync(textFile, "Hello World!");
Getting Files
To retrieve a file from a folder is very easy as long as you know the name of the file you've created within that folder.
To retrieve all the possible files within a folder, you can call the GetFilesAsync method on your folder.
- var files = await newFolder.GetFilesAsync();
- var desiredFile = files.FirstOrDefault(x => x.Name == "text.txt");
Now that we have our file, you're probably asking "How do I get the text out though?". Well we use the FileIO class again to read the text out to a string.
- var textContent = await FileIO.ReadTextAsync(desiredFile);
Saving Media
There are many ways of capturing media within your application. You have MediaCapture that allows you to capture photos, videos and audio. All of these kindly save themselves into a StorageFile that you need to provide from your local folder as I said before. The CreateFileAsync method allows you to create a file with any extension, however the APIs you're hooking into need to be able to understand that file.
Using MediaCapture, you'll need to provide the extension to the CreateFileAsync method that is associated with that media type. For example, creating a photo you would choose to capture to a JPEG file. With a video, you'd opt for a MP4 file and so on.
That's all there is too it folks. Explore some of the features in WinRT and you'll find that you'll be able to save to StorageFiles that you can create in your own local storage folders within your app to retrieve later.
You can grab a sample helper class in the attached Zip files that you can use in your own applications.
If you have any questions, feel free to leave a comment and I will get back in touch!

Gowtham RajamanickamPosted Apr 20, 2016, 8:39 AM
good article...
Ben PagePosted Apr 12, 2016, 1:51 PM
Is this guide still accurate for Windows 10 Universal Applications?
Mukesh KumarPosted Oct 24, 2015, 12:06 PM
Good One
Miguel Angel Ruiz AlvarezPosted Jun 4, 2015, 9:55 AM
Great, thanks for sharing James.