The basics

The package WP.IO contains just two static classes, FileIO and PathIO. If you already know how to use these classes ind Windows Runtime when developing Windows 8 apps, you don't need to read any further. For the rest of you, here's a simple guide.

How to use the FileIO class

FileIO is designed for simple reading/writing of data from/to StorageFiles. Let's say, you already got StorageFile representing file in app isolated storage, you can then read the content like this:
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("myfile.txt");
string content = await FileIO.ReadTextAsync(file);

Yep, that's all. And for writing string into file, you can use:
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("myfile.txt");
await FileIO.WriteTextAsync(file, "hello world");

Again really simple. The FileIO class contains similar methods for reading lines of text...
IList<string> lines= await FileIO.ReadLinesAsync(file);

...appending text to existing file...
await FileIO.AppendTextAsync(file, "hello from Earth");

...appending lines of text to existing file...
await FileIO.WriteLinesAsync(file, new string[] { "Mars", "Sun", Mercury" } );

...writing array of bytes into file...
await FileIO.WriteBytesAsync(file, new byte[] { ... } );

... reading IBuffer from file or writing IBuffer to file.
IBuffer buffer = await FileIO.ReadBufferAsync(file);
await FileIO.WriteBufferAsync(file, (IBuffer)...);

How to use the PathIO class

PathIO is really similar to FileIO with the only difference, that you don't need to create/get StorageFile instance for the operation, you just need Uri of the file. You can then read the content like this:
string content = await PathIO.ReadTextAsync("ms-appdata:///local/myfile.txt");

It's even shorter than using the FileIO class. And for writing string into file, you can use:
await PathIO.WriteTextAsync("ms-appdata:///local/myfile.txt", "hello world");

Again really simple. The PathIO class contains similar methods for reading lines of text...
IList<string> lines= await PathIO.ReadLinesAsync("ms-appdata:///local/myfile.txt");

...appending text to existing file...
await PathIO.AppendTextAsync("ms-appdata:///local/myfile.txt", "hello from Earth");

...appending lines of text to existing file...
await PathIO.WriteLinesAsync("ms-appdata:///local/myfile.txt", new string[] { "Mars", "Sun", Mercury" } );

...writing array of bytes into file...
await PathIO.WriteBytesAsync("ms-appdata:///local/myfile.txt", new byte[] { ... } );

... reading IBuffer from file or writing IBuffer to file.
IBuffer buffer = await PathIO.ReadBufferAsync("ms-appdata:///local/myfile.txt");
await PathIO.WriteBufferAsync("ms-appdata:///local/myfile.txt", (IBuffer)...);

Note when using PathIO for writing stuff, the target file must already exist for this API to succeed.

Last edited Nov 16, 2013 at 4:33 PM by martinsuchan, version 1