title: Append to a Text File (Not Overwrite!)
body: <p>Another Universal Platform App question from me, still learning!</p>
  
  <p>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: <a href="https://msdn.microsoft.com/en-us/library/windows/apps/mt185401.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/windows/apps/mt185401.aspx</a></p>
  
  <p>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?</p>
  
  <pre><code>//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.
  </code></pre>
  
  <p>If it makes any difference, my read code:</p>
  
  <pre><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;
              }
          }
  </code></pre>
  
  <p>Thanks.</p>