本文介绍了UWP 在文档和图片库中保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个 UWP 应用,它可以将保存在他自己存储中的文件导出到文档库中.

I'm trying to make a UWP app which can export a file saved in his own storage into the document library.

在 Package.appxmanifest 中,我插入了以下几行:

In Package.appxmanifest I've inserted the following lines:

<uap:Capability Name="picturesLibrary" />
<uap:Capability Name="documentsLibrary" />

获取路径的代码是这样的:

The code to get the path is this:

StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null /* current user */, KnownFolderId.DocumentsLibrary);
string path = storageFolder.Path + "\\" + fileName;

保存文件的代码是这样的:

The code to save the file is this:

FileStream writer = new FileStream(filePath, FileMode.Create);

此时,程序启动了这个异常:

At this point, the program launches this exception:

Access to the path 'C:\Users\luca9\AppData\Roaming\Microsoft\Windows\Libraries\Documents.library-ms' is denied.

在我的 950XL 上,例外情况类似:

On my 950XL, the exception is similar:

Access to the path 'C:\Data\Users\DefApps\APPDATA\ROAMING\MICROSOFT\WINDOWS\Libraries\Documents.library-ms' is denied.

我已经尝试过文档和图片库,但我遇到了同样的异常.

I've tryied both on Documents and Pictures libraries, but I get the same exception.

我该如何解决?

先谢谢你,

卢卡

推荐答案

不要获取带有路径的 FileStream - 应用没有权限.由于您已经拥有 StorageFolder,请使用它创建一个 StorageFile,然后使用其中一种方法从中获取流,例如:

Don't get FileStream with path - the app doesn't have privileges. As you already have StorageFolder, use it to create a StorageFile and then get stream from it with one of its methods, for example:

var file = await storageFolder.CreateFileAsync("fileName");
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
    // do what you want
}

这篇关于UWP 在文档和图片库中保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:15