Reading and Writing Files in WinRT

I started converting my Windows Phone database project to WinRT. If you read the description of that project, you see that it is using Isolated Storage. There is no Isolated Storage in WinRT, so I had to practically re-write the entire project, at least all parts that deal with persistence. I have a preview now available, but since there is no TFS integration yet in Visual Studio 11 Express preview, I am not quite ready to post the project to CodePlex yet, but I wanted to post it on my blog as early preview.

As part of writing the file access login, I wrote a couple of extension methods for StorageFile class, those that used to exist in .NET. Primarily, I am referring to WriteAllText and ReadAllText methods. It took a bit to figure those out, and I wanted to post my research here for other folks’ benefits.

Let’s take a look at the class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;

namespace WinRTDatabase.IO
{
    /// <summary>
    /// Storage File extensions for reading and writing
    /// </summary>
    public static class StorageFileExtensions
    {
        /// <summary>
        /// Asynchronously write a string to a file
        /// </summary>
        /// <param name="storageFile">StorageFile to write text to</param>
        /// <param name="content">Text to write</param>
        /// <returns>Task/ void if used with await</returns>
        async public static Task WriteAllTextAsync(this StorageFile storageFile, string content)
        {
            var inputStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite);
            var writeStream = inputStream.GetOutputStreamAt(0);
            DataWriter writer = new DataWriter(writeStream);
            writer.WriteString(content);
            await writer.StoreAsync();
            await writeStream.FlushAsync();
        }

        /// <summary>
        /// Asynchronously read a string from a file
        /// </summary>
        /// <param name="storageFile">StorageFile to read text from</param>
        /// <returns>Task/ void if used with await</returns>
        async public static Task<string> ReadAllTextAsync(this StorageFile storageFile)
        {
            string content;
            var inputStream = await storageFile.OpenAsync(FileAccessMode.Read);
            var readStream = inputStream.GetInputStreamAt(0);
            var reader = new DataReader(readStream);
            uint fileLength = await reader.LoadAsync((uint)inputStream.Size);
            content = reader.ReadString(fileLength);
            return content;
        }
    }
}

No color coding yet in VS 2011, sorry for black and white code.  As you can see right away, all file IO must be asynchronous in WinRT at some point.  In both methods I am getting a stream from the file, then creating either input or output stream, then using either DataReader or DataWriter I am getting or setting the content of the file.me

Another interesting aspect is that both methods are asynchronous, but using different return values.  Return value of Task will translate into void method when used with async keyword.  Return value of Task<string> will translate into just string when using with async keyword.

await file.WriteAllTextAsync(content);

or

string content = await file.ReadAllTextAsync();

Stay tuned to sample WinRT application that is using my WinRT database that is based on file system.

2 Comments

Leave a Reply to Sergey Barskiy Cancel reply

Your email address will not be published. Required fields are marked *