Red-Green-Code

Deliberate practice techniques for software developers

  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter

Time Tortoise: Reading and Writing Text Files in UWP Apps

By Duncan Smith Aug 30 0

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)

Categories: TT

Prev
Next

Stay in the Know

I'm trying out the latest learning techniques on software development concepts, and writing about what works best. Sound interesting? Subscribe to my free newsletter to keep up to date. Learn More
Unsubscribing is easy, and I'll keep your email address private.

Getting Started

Are you new here? Check out my review posts for a tour of the archives:

  • 2023 in Review: 50 LeetCode Tips
  • 2022 in Review: Content Bots
  • 2021 in Review: Thoughts on Solving Programming Puzzles
  • Lessons from the 2020 LeetCode Monthly Challenges
  • 2019 in Review
  • Competitive Programming Frequently Asked Questions: 2018 In Review
  • What I Learned Working On Time Tortoise in 2017
  • 2016 in Review
  • 2015 in Review
  • 2015 Summer Review

Archives

Recent Posts

  • Do Coding Bots Mean the End of Coding Interviews? December 31, 2024
  • Another Project for 2024 May 8, 2024
  • Dynamic Programming Wrap-Up May 1, 2024
  • LeetCode 91: Decode Ways April 24, 2024
  • LeetCode 70: Climbing Stairs April 17, 2024
  • LeetCode 221: Maximal Square April 10, 2024
  • Using Dynamic Programming for Maximum Product Subarray April 3, 2024
  • LeetCode 62: Unique Paths March 27, 2024
  • LeetCode 416: Partition Equal Subset Sum March 20, 2024
  • LeetCode 1143: Longest Common Subsequence March 13, 2024
Red-Green-Code
  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter
Copyright © 2025 Duncan Smith