写入和读取文本文件 - C# Windows 通用平台应用程序 Windows 10 [英] Writing to and reading from a text file - C# Windows Universal Platform App Windows 10

查看:28
本文介绍了写入和读取文本文件 - C# Windows 通用平台应用程序 Windows 10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

******** 有效!但是您必须在显示任何内容之前输入 TextBox.我想那是因为我使用了 'TextChanged' 事件处理程序.如果我想让它在没有用户交互的情况下显示文本文件的内容,我应该使用哪个事件处理程序?********

所以我想在按下按钮时将一些数据写入 C# Windows 10 通用平台应用程序中的文本文件,并且我想要一个 TextBlock 或一个 TextBox 在我的应用程序中读取该文本文件的内容.

我正在使用一个枢轴样式的应用程序,写入文件的按钮位于一个枢轴上,而我想要包含文本文件内容的 TextBlock 或 TextBox 位于另一个枢轴上.

我的代码如下.它不起作用.我不确定它是否在创建和写入文件,并且我的 TextBox 或 TextBlock 中没有任何内容.:(

我从这里得到了代码:https://msdn.microsoft.com/en-us/library/windows/apps/mt185401.aspx

写入文件的代码:

private async void submitButton_Click(object sender, RoutedEventArgs e){//将票写入本地数据库(txt)////创建文本文件来保存数据Windows.Storage.StorageFolder storageFolder =Windows.Storage.ApplicationData.Current.LocalFolder;Windows.Storage.StorageFile 票证文件 =await storageFolder.CreateFileAsync("tickets.txt",Windows.Storage.CreationCollisionOption.ReplaceExisting);//将数据写入文件等待 Windows.Storage.FileIO.WriteTextAsync(ticketsFile, "Swift as a shadow");}

在文本框中读取文件的代码:

private async void viewTickets_TextChanged(object sender, TextChangedEventArgs e){Windows.Storage.StorageFolder storageFolder =Windows.Storage.ApplicationData.Current.LocalFolder;Windows.Storage.StorageFile 票证文件 =等待 storageFolder.GetFileAsync("tickets.txt");字符串savedTickets = await Windows.Storage.FileIO.ReadTextAsync(ticketsFile);viewTickets.Text = savedTickets;}

解决方案

你的代码非常好,唯一的问题是它没有被执行.当您点击 按钮 时,您的文件就被创建了.但是您没有在 textbox 中输入任何内容,因此您永远不会阅读文件.

我认为您想在写完后立即阅读.将 Read 文件代码放在 Write 代码之后:

private async void submitButton_Click(object sender, RoutedEventArgs e){//将票写入本地数据库(txt)////创建文本文件来保存数据Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;Windows.Storage.StorageFile ticketFile = await storageFolder.CreateFileAsync("tickets.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);//将数据写入文件等待 Windows.Storage.FileIO.WriteTextAsync(ticketsFile, "Swift as a shadow");//读取文件字符串savedTickets = await Windows.Storage.FileIO.ReadTextAsync(ticketsFile);viewTickets.Text = savedTickets;}

并删除 viewTickets_TextChanged 事件处理程序.

********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; 
    }

解决方案

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.

这篇关于写入和读取文本文件 - C# Windows 通用平台应用程序 Windows 10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆