问题
When closing the UWP I will get a list of items from a database. I want to save this list in a file which will be created/edited in the documents folder.
I tried to gain access to the Documents folder by this:
StorageFolder stFo = KnownFolders.DocumentsLibrary;
My Problem is that I have no access to the Documents Folder ('An exception of type "System.UnauthorizedAccessException" occured...').
Now I have read that you could get access by adding some lines to Package.appxmanifest
<Package xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities">
and
<Capabilities>
<rescap:Capability Name="appCaptureSettings"/>
<uap Name="documentsLibrary"/>
</Capabilities>
I tried this but it doesn't work.
Is there another way to gain access? (If yes, an example for it would be awesome)
edit
This is probably important: This UWP is sideloaded
回答1:
It's not enough to add the declare the documentsLibrary
capability. You also need to add file type associations to your app manifest to declare what specific file types your app can access: https://social.msdn.microsoft.com/Forums/ie/en-US/7ee61fd7-db6d-49a6-bda4-701105a500e7/uwpcant-access-documents-library-in-universal-windows-app?forum=wpdevelop
Note that the Documents library is not intended for general use though.
You should consider using ApplicationData.LocalFolder
or LocalCacheFolder
, RoamingFolder
, SharedLocalFolder
, or TemporaryFolder
based on how your data should be stored.
来源:https://stackoverflow.com/questions/56289334/how-do-i-gain-access-to-the-documents-folder-uwp