Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to and reading from a text file - C# Windows Universal Platform App Windows 10

Tags:

c#

text

********IT WORKS! But you have to type in the TextBox before anything shows. I guess that's because I used the 'TextChanged' event handler. Which event handler do I use if I want it to show the contents of the text file without user interaction?********

So I want to write some data to a text file in a C# Windows 10 Universal Platform App when a button is pressed, and I want a TextBlock or a TextBox to read the contents of that text file in my app.

I'm using a pivot-style app, the button to write the file is on one pivot and the TextBlock or TextBox I want to contain the contents of the text file is on another pivot.

My code is below. It doesn't work. I'm not sure it's even creating and writing the file and there is nothing in either my TextBox or TextBlock. :(

I got the code from here: https://msdn.microsoft.com/en-us/library/windows/apps/mt185401.aspx

Code to write the file:

private async void submitButton_Click(object sender, RoutedEventArgs e)
    {
        //WRITE THE TICKET TO A LOCAL DATABASE (txt)//

        //Create the text file to hold the data
        Windows.Storage.StorageFolder storageFolder =
            Windows.Storage.ApplicationData.Current.LocalFolder;
        Windows.Storage.StorageFile ticketsFile =
            await storageFolder.CreateFileAsync("tickets.txt",
                Windows.Storage.CreationCollisionOption.ReplaceExisting);

        //Write data to the file
        await Windows.Storage.FileIO.WriteTextAsync(ticketsFile, "Swift as a shadow");
    }

Code to read the file in a TextBox:

private async void viewTickets_TextChanged(object sender, TextChangedEventArgs e)
    {
        Windows.Storage.StorageFolder storageFolder =
            Windows.Storage.ApplicationData.Current.LocalFolder;
        Windows.Storage.StorageFile ticketsFile =
            await storageFolder.GetFileAsync("tickets.txt");

        string savedTickets = await Windows.Storage.FileIO.ReadTextAsync(ticketsFile);

        viewTickets.Text = savedTickets; 
    }
like image 425
Jason Brown Avatar asked Dec 30 '15 16:12

Jason Brown


People also ask

What are the C function to read and write in a file in text mode?

You can even use fputs(). Only strings can be read or write instead of integers, float or characters. 18) What are the C functions used to read or write a file in Binary Mode.? fwrite(pointer, size, count , filepointer);

What are the C function used to read and write?

Getc and putc functions are used to read and write a single character. We can write to a file after creating its name, by using the function fprintf() and it must have the newline character at the end of the string text.


Video Answer


1 Answers

Your code is perfectly fine, the only problem is it doesn't get executed. When you click on the button your file is created. But you don't type anything in the textbox so you never read the file.

I think you want to read it immediately after writing it. Put your Read file code right after the Write code:

private async void submitButton_Click(object sender, RoutedEventArgs e)
{
    //WRITE THE TICKET TO A LOCAL DATABASE (txt)//

    //Create the text file to hold the data
    Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    Windows.Storage.StorageFile ticketsFile = await storageFolder.CreateFileAsync("tickets.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);

    //Write data to the file
    await Windows.Storage.FileIO.WriteTextAsync(ticketsFile, "Swift as a shadow");

    //read file
    string savedTickets = await Windows.Storage.FileIO.ReadTextAsync(ticketsFile);

    viewTickets.Text = savedTickets;
}

and remove the viewTickets_TextChanged event handler.

like image 134
Shaharyar Avatar answered Oct 23 '22 14:10

Shaharyar