File Manipulation in Windows 8 Store Apps

Posted by: Sumit Maitra , on 10/13/2012, in Category WinRT
Views: 65865
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-Layout

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:

create-file-clicked

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 ‘Sample.data’

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.

open-read-clicked

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

append-save-clicked

  • 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. 

delete-file-click

Conclusion

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)

Give me a +1 if you think it was a good article. Thanks!
Recommended Articles
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


User Feedback
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 http://www.wiziq.com/course/46-INTERVIEW-PREPARATION-COURSE-BY-SHIVPRASAD-KOIRALA 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 asp.net c# javascript is here http://www.interviewqsn.com
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??

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel