在 UWP 中哪里存储用户可访问的文件?

Where to store user-accessible files in UWP?

我这里有一个跨平台应用程序,它使用 DependencyService 获取我的日志文件的文件路径。这适用于 ApplicationData.Current.LocalCacheFolder.Path,但现在日志文件应该可供用户访问。这个想法是用户将他的设备插入 PC,从中复制日志文件,然后通过普通电子邮件将其发送给我。 (目前,不打算通过商店分发该应用程序,并且不能保证用户在其设备上设置了电子邮件帐户。)

首先,我尝试使用 KnownFolders.DocumentsLibrary,但在这里我得到 Access is denied。如果我查看 documentation,这个文件夹不适合我使用。其他位置似乎也不合适。

这种方法在 UWP 中可行吗?

您需要添加 documentsLibrary 中的 Capabilities 才能访问 KnownFolders.DocumentsLibrary

要添加,请转到 YourApp.UWP > "Capability" 选项卡中的 "Package.appxmanifest" 并检查要存储的功能。示例:"Picture Library" 或 "Removable Storage"

新回答者:

我发现,Access denied 只出现在桌面上,不会出现在移动设备上。之后,我发现 this post, which describes why this does happen. It's because of the permission handling 并且我放弃了我的权限。有几种可能性如何处理这种情况:

示例:

FolderPicker folderPicker = new FolderPicker();
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
    StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
}
StorageFolder newFolder;

newFolder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("PickedFolderToken");
await newFolder.CreateFileAsync("test.txt");

示例:

StorageFolder tempFolder = await StorageFolder.GetFolderFromPathAsync(Path.Combine(ApplicationData.Current.LocalCacheFolder.Path, "YourApp"));
StorageFile tempFile = await tempFolder.CreateFileAsync(Path.GetFileName(pathToAttachment), CreationCollisionOption.ReplaceExisting);
await file.CopyAndReplaceAsync(tempFile);

旧答案:

我目前的解决方案是在我的应用程序中提供一个按钮,该按钮通过 DependencyService 本地调用 FolderPicker,并且仅在 UWP 上调用。有了这个,用户可以 select 位置,我将文件复制到这个位置。效果很好,尽管我希望我不必只为一个平台做某事。