Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:46:48+00:00 2026-06-11T02:46:48+00:00

Basiclly, my code is a very simple test for write and read files in

  • 0

Basiclly, my code is a very simple test for write and read files in a windows 8 style app. Here, a string “Jessie” is firstly written to dataFile.txt, and then it is read by the program so that the Text property of a Textblock in xaml could be updated.

From msdn samples, I knew that WriteTextAsync and ReadTextAsync are for dataFile access to file in Windows 8 programming. However, the test result is that WriteTextAsync function actually works whereas ReadTextAsync does not(I can see the real txt file that “Jessie” has been written to dataFile.txt, however the variable str is always null).

I have seen similar questions from internet, but none of the answers for them make sense to me. Many answer mentioned the problem might be ReadTextAsync is a async function, such like using Task but all of their solutions doen’t work to my code. My question is how can I get the return value synchronously using ReadTextAsync or is there any other method to read data from txt file in Windows8 app so that I can update the UI synchronously?

Here is code:

public sealed partial class MainPage : Page
{  
    Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

    public MainPage()
    {
        this.InitializeComponent();

        Write();
        Read();
    }

    async void Write()
    {
        StorageFile sampleFile = await localFolder.CreateFileAsync("dataFile.txt", Windows.Storage.
                  CreationCollisionOption.ReplaceExisting);

        await FileIO.WriteTextAsync(sampleFile, "Jessie");
    }

    async void Read()
    {
        try
        {      
            StorageFile sampleFile = await localFolder.GetFileAsync("dataFile.txt");
            string str = await FileIO.ReadTextAsync(sampleFile);

            Text.DataContext = new Test(str, 1);
        }
        catch (FileNotFoundException)
        {

        }
    }

    public class Test
    {
        public Test() { }

        public Test(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public string Name { get; set; }
        public int Age { get; set; }

        // Overriding the ToString method
        public override string ToString()
        {
            return "Name: " + Name + "\r\nAge: " + Age;
        }
    }
}

Thanks very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:46:50+00:00Added an answer on June 11, 2026 at 2:46 am

    You should have your async methods return Task rather than void whenever possible. You may find my intro to async/await post helpful. I also have a blog post on asynchronous constructors.

    Constructors cannot be async, but you can start an asynchronous process from a constructor:

    public MainPage()
    {
      this.InitializeComponent();
      Initialization = InitializeAsync();
    }
    
    private async Task InitializeAsync()
    {
      await Write();
      await Read();
    }
    
    public Task Initialization { get; private set; }
    
    async Task Write() { ... }
    async Task Read() { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making very simple test app, just to see how drag & drop
I have a very simple tab switching code. Basically HTML looks like this: <ul
Basically, Im just trying out a very simple collision test for another program Im
I have a very simple jquery test page and I can't seem to get
I wrote the following very simple python code to find circles in an image:
This code basically translates characters based on position in one string to the character
My code basically needs to start up a simple chat server with a client.
Basically what I'm trying to do is make a very simple vertical bullet projectile
I have a very simple web form which consists of labels and input fields,
i've got a (probably very simple) problem with jquery. I'm running a Java Servlet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.