Time Tortoise: Reading and Writing Text Files in UWP Apps

Text

This is one in a series of articles about Time Tortoise, a Universal Windows Platform app for planning and tracking your work schedule. For more on the development of this app and the ideas behind it, see my Time Tortoise category page.

As described last week, I’m starting to build infrastructure for a text file UI: a way to use text files for user input and program output, rather than relying completely on the graphical XAML UI. This week, I’m looking into the specifics of text file I/O for UWP apps.

Features

To start with, I’m targeting the following two features for my text file UI:

  • Read settings: Read configuration values from a user-friendly input file. An example of a setting: the number of seconds to wait before a user is considered idle.
  • Write a daily summary: Write a text file containing statistics about the current day’s time tracking, such as how much time was spent on each activity.

File Locations

For security reasons, Windows restricts where apps can write files. Console apps (both the Classic and .NET Core varieties) can write to places like D:\ and C:\MyFolder. But they aren’t allowed to write to C:\ or C:\Program Files. UWP apps are more restricted. Unless a user selects a location using a file picker, they aren’t allowed to write to arbitrary locations.

For the text file UI, I’m planning to read/write to %LocalAppData%\Packages\[package_name]\LocalState. This is where the database file is, so I’ll be keeping all of the important Time Tortoise data files together.

Asynchronous File Operations

According to the MVVM philosophy of minimizing code in the View layer, file I/O code should live in the Model or View Model and be called from the View if necessary.

Doing this with a method like File.WriteAllText produces the following error:

System.InvalidOperationException occurred

Message=Synchronous operations should not
be performed on the UI thread.  Consider
wrapping this method in Task.Run.

This makes sense. Writing a file to disk might take some time, which might make the user think the app isn’t responding (not to mention making them wait).

There are a couple ways to resolve this problem. One is just to follow the instructions in the error message:

Task.Run(() =>
    System.IO.File.WriteAllText([path], [content])
);

Another option is to use file I/O methods that are inherently asynchronous:

using (var stream =
    new FileStream([path], FileMode.Create))
using (var writer = new StreamWriter(stream))
    writer.WriteLineAsync([content]);

Either way works, but the second one is probably more flexible.

Text Files and UWP

Compared to classic Windows apps, UWP apps have to be a bit more careful about writing files to the local machine, but there’s no reason why file I/O can’t be used to enhance a feature without spending the extra time to add to the graphical UI.

(Image credit: Michael Riedel)