Nov 142012
 November 14, 2012  Posted by at 10:08 pm C# & F#, Telerik, WinRT Tagged with: ,  Add comments

To read the other steps in the step by step guide for creating a Metro/ WinRT application go here

Welcome 🙂 We are currently at step 15 in our step by step guide for creating a Windows Store Application- if this is the first post you’ve seen in this series I would recommend that you have a look at the other posts if you want to code along. Last time we added layout awareness and handled the different modes, and today we want to let the user export the data for the study activites.

You might have noticed that there was an export button in the last tutorial. We want the user to be able to export the study activities to a text file, so we have added a button for that function.
The user clicks on the button, a generic filepicker windows opens set to the documents folder and available documents limited to documents with the .txt extension. The user can select an existing file and overwrite it, or create a new one.

Since the user is in full control we don’t need to set this as a capability in the app manifest. If we were to automatically save to a file on the users system (not local in the app) we would have to declare this in the app manifest.

Working with files throught the filepicker is just as easy as with WPF and SL, but a bit different. You create a FileSavePicker, and set a startup location and filetype accepted, check if a file was selected and try to write to that file. Id something was to go wrong, we notify the user and let them try again, if no file is selected we notify.

Code for the filepicker:

[sourcecode language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using Mihsp.Model;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Popups;

namespace Mihsp.Common
public static class FileHelper
public static async void SaveAsync(IEnumerable<StudyActivity> userActivities)
var savePicker = new FileSavePicker
SuggestedStartLocation = PickerLocationId.DocumentsLibrary

savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });

StorageFile file = await savePicker.PickSaveFileAsync();

if (file != null)
await FileIO.WriteLinesAsync(file, FormatText(userActivities));
DisplayMessage("Something went wrong, please try again");
DisplayMessage("No file selected, please try again");

static internal IEnumerable<string> FormatText(IEnumerable<StudyActivity> userActivities)
new List<string>(
activity =>
"\"{0}\"\r\n{1}\r\n——Comment——\r\n{2}\r\n——————-\r\nTime est. :\t {3}\r\nTime spent:\t {4}\r\nWorth:\t\t {5}\r\nImportance:\t {6}\r\nUrgency:\t {7}\r\n\r\n",

static async void DisplayMessage(string message)
var messageDialog = new MessageDialog(message);
await messageDialog.ShowAsync();

And code behind:

[sourcecode language=”csharp”]
private void Export_Click(object sender, RoutedEventArgs e)

Rather easy, and a very neat feature- I know how frustrated I get when I can’t export data. And wouldn’t it be nice if we implemented a share contract in the app so the list of activities can be shared? That is what we will be doing next time!

  One Response to “WinRT app guide: Step 15: Exporting data as a text file”

  1. Just wanted to post a: Thank you!

    Very nice Series

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



What is 7 + 11 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)