当前位置:  开发笔记 > 编程语言 > 正文

写入和读取文本文件 - C#Windows Universal Platform App Windows 10

如何解决《写入和读取文本文件-C#WindowsUniversalPlatformAppWindows10》经验,为你挑选了1个好方法。

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

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

我正在使用一个pivot样式的应用程序,在一个数据透视表上写入文件的按钮和我想要包含文本文件内容的TextBlock或TextBox在另一个数据透视表上.

我的代码如下.它不起作用.我不确定它是否创建和编写文件,我的TextBox或TextBlock都没有.:(

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

编写文件的代码:

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");
    }

用于在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; 
    }

Shaharyar.. 8

你的代码非常好,唯一的问题是它没有被执行.当您单击时,button您的文件已创建.但是你没有输入任何东西,textbox所以你永远不会读取文件.

我想你想在写完之后立即阅读它.将您的Read文件代码放在代码后面Write:

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

并删除viewTickets_TextChanged事件处理程序.



1> Shaharyar..:

你的代码非常好,唯一的问题是它没有被执行.当您单击时,button您的文件已创建.但是你没有输入任何东西,textbox所以你永远不会读取文件.

我想你想在写完之后立即阅读它.将您的Read文件代码放在代码后面Write:

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

并删除viewTickets_TextChanged事件处理程序.

推荐阅读
mobiledu2402852413
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有