我正在寻找一种将字符串文本附加到 Windows 应用商店应用程序中的文件的方法。我曾尝试读取该文件,然后创建一个新文件来覆盖它,但 Windows Store Apps C# 的工作方式与 C 不同,在 C 中创建同名的新文件会覆盖旧文件。目前我的代码正在打开旧文件,读取它的内容,删除它并用我阅读的内容加上我希望附加的内容创建一个新文件。 我知道有更好的方法,但我似乎找不到。那么如何将文本附加到 Windows 应用商店应用程序 (Windows RT) 中已存在的文件?
编辑--
我试过了
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("feedlist.txt");
await Windows.Storage.FileIO.AppendTextAsync(file, s);
但我不断收到 System.UnauthorizedAccessException
根据 MSDN,当文件是只读的(我用右键单击属性检查过,不是)并且如果我没有访问该文件的必要权限,就会发生这种情况
我该怎么办?
最佳答案
您可以使用 FileIO要附加到文件的类。例如……
// 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");
查看 File Access Sample更多示例。
关于c# - 将文本附加到文件 Windows 商店应用程序 (windows RT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14711453/