Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending Text to File Windows store Apps (windows RT)

I am looking for a way to append strings-text to a file in a Windows Store App. I have tried reading the file and then creating a new one to overwrite it but Windows Store Apps C# does not work like C where when creating a new file with the same name overwrites the old one. Currently my code is opening the old file, reading it's contents, deleting it and creating a new one with the content I read plus the content I wish to append. I know there is a better way but I cannot seem to find it. So How may I append text to an already existent file in a Windows Store App (Windows RT)?

EDIT--

I tried this

var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("feedlist.txt");
await Windows.Storage.FileIO.AppendTextAsync(file, s);

but I keep getting System.UnauthorizedAccessException according to MSDN this happens when the file is readonly (I checked with right click properties, it's not) and if I do not have the necessary privileges to access the file what should I do?

like image 308
John Demetriou Avatar asked Feb 05 '13 15:02

John Demetriou


1 Answers

You can use the FileIO class to append to a file. For example ...

// Create a file in local storage
var folder = ApplicationData.Current.LocalFolder;
var file = await folder.CreateFileAsync("temp.txt", CreationCollisionOption.FailIfExists);

// Write some content to the file
await FileIO.WriteTextAsync(file, "some contents");

// Append additional content
await FileIO.AppendTextAsync(file, "some more text");

Check out the File Access Sample for more examples.

like image 81
JP Alioto Avatar answered Sep 28 '22 00:09

JP Alioto