File Manipulation in Windows 8 Store Apps
Posted by: Sumit Maitra
in Category Windows Store Apps
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.
We use the Blank Windows Store App template to create a new application. The Application layout is as follows:
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 ‘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.
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.
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!