哇,这种方式是否比它需要的更复杂。有人可以向我解释为什么以下代码有效:
string stringToWrite = "SomeStuff";
Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
var files = await installedLocation.GetFilesAsync();
foreach (Windows.Storage.StorageFile sf in files)
{
if (sf.Name.Equals("log.txt"))
{
await FileIO.AppendTextAsync(sf, stringToWrite);
}
}
然而,以下因 AccessDenied 而失败:
Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
var log = await installedLocation.GetFileAsync("log.txt");
await FileIO.AppendTextAsync(log, stringToWrite);
唯一的区别是循环通过 GetFilesAsync 方法返回的文件与按名称获取文件。顺便说一句,按名称获取文件是有效的,因为如果我在 GetFileAsync 中拼错 log.txt,我会得到一个异常。
很混乱....