简体   繁体   中英

Append to a Text File (Not Overwrite!)

Another Universal Platform App question from me, still learning!

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. Here are the different ways of creating and writing to a text file: 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. For example method AppendTextAsync :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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