c# - 将文本附加到文件 Windows 商店应用程序 (windows RT)

标签 c# windows-8 windows-runtime windows-store-apps

我正在寻找一种将字符串文本附加到 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/

相关文章:

c# - 从 LibGit2Sharp 中的提交中获取修改/添加/删除的文件

c# - 在发布版本中反射(reflect)堆栈跟踪是否危险?

带非托管 C++ Unicode DLL 链接到非托管 C++ ANSI DLL 的 C# EXE 崩溃

c# - C# sqlconnector 上的未知登录

windows-8 - 升级到 Windows 8 后 TortoiseSVN 无法识别 svn 文件夹

c# - 在 WinRT 应用程序中即时本地化

xaml - 在 Metro 应用程序中使用来自另一个程序集的样式

c# - RunAsync - 如何等待 UI 线程上的工作完成?

c# - Windows 8.1 应用程序中的 FlipView 渲染问题

c# - 获取设置为自动的网格的实际宽度