How to enable DocumentsLibrary capability in a Win

2019-02-17 14:14发布

I just created a new blank XAML/C# Windows Store app in Visual Studio. I tried to create a file in the Documents folder with this code:

// DEBUG ONLY:
StorageFile file = await KnownFolders.DocumentsLibrary.CreateFileAsync("Hey lol.txt");

But it throws this exception (which I expected):

WinRT information: Access to the specified location (DocumentsLibrary) requires a capability to be declared in the manifest.

Which is fine. I expected it. So I go to Package.appxmanifest and go to Capabilities tab, and to my surprise, there is no "DocumentsLibrary" capability listed.

How do I enable it if it's not even there?

enter image description here

4条回答
干净又极端
2楼-- · 2019-02-17 14:15

Looks like your answer is here. The author shows it available in VS2012, but gone from the list in VS2013, citing MS policy against accessing that particular folder.

[Although] this capability is gone just from the UI, you still can open appxmanifest source and manually add the capability. The result will probably be the same as before – failure of certification for individual developers, so you better stay away from this trick. Microsoft strongly recommend against using Documents Library capability, suggesting Folder and File Pickers instead.

查看更多
看我几分像从前
3楼-- · 2019-02-17 14:22

As this is UAP the syntax needs to state this, should be as followed

  <Capabilities>
    <Capability Name="internetClient" />
    <uap:Capability Name="documentsLibrary" />
  </Capabilities>

You need to add 'uap:' in front of Capability

查看更多
Root(大扎)
4楼-- · 2019-02-17 14:23

Here's what you'll get from the Windows Store when you try to publish it

enter image description here

查看更多
放荡不羁爱自由
5楼-- · 2019-02-17 14:35

As per Grant's answer, a way around this is to add the Capability manually.

Right-click the Package.appxmanifest file in Solution Explorer, and select "View code", then either find the Capabilities element, or add it yourself:

...
  <Capabilities>
    <Capability Name="internetClient" />
    <Capability Name="removableStorage" />
    <Capability Name="documentsLibrary" />
  </Capabilities>
</Package>
查看更多
登录 后发表回答