File Manipulation in Windows 8 Store Apps

Posted by: Sumit Maitra , on 10/13/2012, in Category Windows Store Apps
Views: 104339
Abstract: The IO Subsystem in WinRT has been written ground up to be completely async.In this article, we see how we can manage (Create and Update) binary data in Files using the Windows 8 runtime.

As we know now, Windows 8 Store Apps are a new category of desktop apps that are touch-centric and run on the new WinRT runtime on Windows 8. In this article, we see how we can manage (Create and Update) binary data in Files using the Windows 8 runtime.

File Management in WinRT

WinRT’s IO Subsystem has been written ground up to be completely async. As a result the System.IO has been superseded by Windows.Storage. Some namespaces from System.IO are still available and we have a set of new async helpers for the existing IO calls.

In this example, we will see how we can create a new file, add data to it, save it, reopen it and append some more data to it.


The Application

We use the Blank Windows Store App template to create a new application. The Application layout is as follows:


File Creation

The ‘Create File’ button, creates a new file and overwrites one if it already exists. The Code behind for this is as follows:


We retrieve the current Application’s LocalFolder first. This is now represented at a StorageFolder, a class from the Windows.Storage Namespace. Here filename is a constant set to ‘’

The CreateFileAsync method call creates the new file and saves the file handle in the sampleFile variable.

Loading/Reading from a File

To load a file we use first check if the file handle (sampleFile) is null or not. If null we use the GetFileAsync method to load the file.


If the file is not present it will set sampleFile to null, else it will load the file.

Once we have the file handle, in the ReadFromFile method, we open instance of an object that implements IRandomAccessStream. If the stream length is > 0 we create a byte array or equal size. We then use the ReadAsync method to read from the randomAccessStream into the byte array

Next we convert the byte array into a string using the UTF8 encoding. In our case we are the ones writing to the file so we know that the Encoding is UTF8.

Making changes and Saving the File

To save changes to a file we take the following steps


  • We get the content from the InputTextBox.
  • Next we open a RandomAccessStream again but this time in read-write mode.
  • From the RandomAccessStream we extract a BCL Stream using the AsStreamForWrite() extension method.
  • We check if the stream has any content, and if it does move the stream’s current pointer the end of the stream by using the Seek command.
  • Now we convert the input string into a Byte Array.
  • Next we increase the length of the stream to accommodate the new data.
  • Finally we write to the stream, since we already moved the current pointer to the end of the stream the new bytes are written at the end of the existing stream and Flush it to disk.

Deleting a File

Deleting a file is now an async operation. We check if the file handle is available. If not we get the file handle using the OpenFile method. Once we have the handle we simply call the DeleteAsync() method to delete the file. 



The File Access samples from MSDN provide good example of basic functions but it does not cover ‘appending’ of data. In current example, we saw how we can append data to end of a stream. This can be useful in case we are continuously making changes to a file stream.

Download the source code (click zip)

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Sumit is a .NET consultant and has been working on Microsoft Technologies since his college days. He edits, he codes and he manages content when at work. C# is his first love, but he is often seen flirting with Java and Objective C. You can follow him on twitter at @sumitkm or email him at sumitkm [at] gmail

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Sean Feldman on Saturday, October 13, 2012 10:32 AM
Thank you for sharing this article/code.

Unrelated to the article - one thing that doesn't look appealing (though it has a better performance) is the whole await/async syntax that feels very unnatural in C# syntax. Perhaps it's just my mind that didn't get adjusted yet :)
Comment posted by P Fennymore on Thursday, October 18, 2012 12:14 AM
Where can I download the SDK to create this app? After reading your tut I am getting ideas ;-)
Comment posted by sarabjeet on Thursday, November 22, 2012 4:52 AM
Great work, really appreciate your effort for the information and being enrolled in the OOps, .NET and SQL INTERVIEW Preparation i found your information very helpful.
Comment posted by Bharti Gogia on Sunday, November 25, 2012 9:59 AM
Hi good question for c# javascript is here
Comment posted by Lakhan Paryani on Wednesday, May 22, 2013 10:10 AM
Very good article,
Though Does it work for excel file too??
I am looking something by which I could read the excel stream and upload it to server, and then manipulate it on server and then send stream to user end convert it to excel back..
any idea??
Comment posted by Lukan on Friday, March 20, 2015 9:48 PM
Hello: I have a Customer class that with name lastname and so on, I like to add and image property to this class,Is it possible you can post a sample for saving an image to SQLite in a windows store app using xaml?
Thanks in advance for any help provided, I will really appreciated and I'm sure many other people will too.