简体   繁体   English

附加到文本文件(不覆盖!)

[英]Append to a Text File (Not Overwrite!)

Another Universal Platform App question from me, still learning! 另一个Universal Platform App问我,还在学习!

Quick question - which method of writing data to a text file do I need to use to append (not overwrite) a string to a text file? 快速问题 - 我需要使用哪种方法将数据写入文本文件,以便将字符串附加(而不是覆盖)到文本文件中? Presently I'm using an async void using streams but it's overwriting my files. 目前我正在使用一个使用流的异步void,但它覆盖了我的文件。 Here are the different ways of creating and writing to a text file: https://msdn.microsoft.com/en-us/library/windows/apps/mt185401.aspx 以下是创建和写入文本文件的不同方法: https//msdn.microsoft.com/en-us/library/windows/apps/mt185401.aspx

Presently this is my code. 现在这是我的代码。 Does it need modifying to append the data to a text file rather than overwrite it, or do I need to use one of the other methods to append data? 是否需要修改以将数据附加到文本文件而不是覆盖它,或者我是否需要使用其他方法之一来附加数据?

//WRITE
        //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.OpenIfExists);

        //Use stream to write to the file
        var stream = await ticketsFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);

        using (var outputStream = stream.GetOutputStreamAt(0))
        {
            using (var dataWriter = new Windows.Storage.Streams.DataWriter(outputStream))
            {
                dataWriter.WriteString("Submitted: " + submitDate + "\n" + "Problem: " + problem + "\n" + "Room: " + location + "\n" + "Description: " + descriptionText.Text + "\n" + "Priority: " + priority + "\n");
                await dataWriter.StoreAsync();
                await outputStream.FlushAsync();
            }
        }
        stream.Dispose(); // Or use the stream variable (see previous code snippet) with a using statement as well.

If it makes any difference, my read code: 如果它有任何区别,我的阅读代码:

//READ
        //Open the text file
        stream = await ticketsFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
        ulong size = stream.Size;

        using (var inputStream = stream.GetInputStreamAt(0))
        {
            using (var dataReader = new Windows.Storage.Streams.DataReader(inputStream))
            {
                uint numBytesLoaded = await dataReader.LoadAsync((uint)size);
                string savedTickets = dataReader.ReadString(numBytesLoaded);

                textBox.Text = savedTickets;
            }
        }

Thanks. 谢谢。

我没有在我面前的平台我现在可以测试,但是这应该可以获得现有文件的长度并将编写器放在最后(新文件应该返回0):

using (var outputStream = stream.GetOutputStreamAt(stream.Size))

Apart from getting a stream, you can also make use of FileIO class. 除了获取流之外,您还可以使用FileIO类。 For example method AppendTextAsync : 例如方法AppendTextAsync

await FileIO.AppendTextAsync(ticketsFile, "Submitted: " + submitDate + "\n" + "Problem: " + problem + "\n" + "Room: " + location + "\n" + "Description: " + descriptionText.Text + "\n" + "Priority: " + priority + "\n");

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

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