简体   繁体   English

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

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

********IT WORKS! ********有用! But you have to type in the TextBox before anything shows. 但是你必须在任何显示之前输入TextBox。 I guess that's because I used the 'TextChanged' event handler. 我猜那是因为我使用了'TextChanged'事件处理程序。 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. 因此,当按下按钮时,我想将一些数据写入C#Windows 10通用平台应用程序中的文本文件,并且我希望TextBlock或TextBox在我的应用程序中读取该文本文件的内容。

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

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. 我不确定它是否创建和编写文件,我的TextBox或TextBlock都没有。 :( :(

I got the code from here: https://msdn.microsoft.com/en-us/library/windows/apps/mt185401.aspx 我从这里得到了代码: 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: 用于在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. 单击button将创建文件。 But you don't type anything in the textbox so you never read the file. 但是您不在textbox键入任何内容,因此您永远不会读取该文件。

I think you want to read it immediately after writing it. 我想你想在写完之后立即阅读它。 Put your Read file code right after the Write code: 将您的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;
}

and remove the viewTickets_TextChanged event handler. 并删除viewTickets_TextChanged事件处理程序。

public async Task WriteToDictionaryAsync(Dictionary<string, string> dictionary, StorageFile binarySourceFile)
    {
        await Task.Delay(5);
        using (FileStream fs = File.OpenWrite(binarySourceFile.Path))
        using (BinaryWriter writer = new BinaryWriter(fs))
        {
            // Put count.
            writer.Write(dictionary.Count);
            // Write pairs.
            foreach (var pair in dictionary)
            {
                writer.Write(pair.Key);
                writer.Write(pair.Value);
            }
        }
    }
    public async void WriteToDictionary(Dictionary<string, string> dictionary, StorageFile binarySourceFile)
    {
        await Task.Delay(5);
        using (FileStream fs = File.OpenWrite(binarySourceFile.Path))
        using (BinaryWriter writer = new BinaryWriter(fs))
        {
            // Put count.
            writer.Write(dictionary.Count);
            // Write pairs.
            foreach (var pair in dictionary)
            {
                writer.Write(pair.Key);
                writer.Write(pair.Value);
            }
        }
    }
    public async Task<Dictionary<string, string>> ReadDictionaryAsync(StorageFile binarySourceFile)
    {
        await Task.Delay(5);
        var result = new Dictionary<string, string>();
        using (FileStream fs = File.OpenRead(binarySourceFile.Path))
        using (BinaryReader reader = new BinaryReader(fs))
        {
            // Get count.
            int count = reader.ReadInt32();
            // Read in all pairs.
            for (int i = 0; i < count; i++)
            {
                string key = reader.ReadString();
                string value = reader.ReadString();
                result[key] = value;
            }
        }
        return result;
    }
    public Dictionary<string, string> ReadDictionary(StorageFile binarySourceFile)
    {
        var result = new Dictionary<string, string>();
        using (FileStream fs = File.OpenRead(binarySourceFile.Path))
        using (BinaryReader reader = new BinaryReader(fs))
        {
            // Get count.
            int count = reader.ReadInt32();
            // Read in all pairs.
            for (int i = 0; i < count; i++)
            {
                string key = reader.ReadString();
                string value = reader.ReadString();
                result[key] = value;
            }
        }
        return result;
    }

//Create bin File var binarySourceFile = await StorageData.LocalFolder.CreateFileAsync("binFile" + ".bin", CreationCollisionOption.ReplaceExisting); //创建bin文件var binarySourceFile = await StorageData.LocalFolder.CreateFileAsync(“binFile”+“。bin”,CreationCollisionOption.ReplaceExisting);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM