Jason Brown
Jason Brown

Reputation: 109

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.

Upvotes: 4

Views: 4378

Answers (2)

Romasz
Romasz

Reputation: 29792

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

Upvotes: 6

Paul Abbott
Paul Abbott

Reputation: 7211

I don't have a platform in front of me I can test right now, but this should work to get the length of the existing file and put the writer at the end (a new file should return 0):

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

Upvotes: 6

Related Questions