UWP save file in Documents and Pictures Library

五迷三道 提交于 2019-12-10 11:35:56

问题


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

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.

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.

How can I solve it?

Thank you in advance,

Luca


回答1:


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
}


来源:https://stackoverflow.com/questions/41870125/uwp-save-file-in-documents-and-pictures-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!