How to gain access to KnownFolders.DocumentsLibrary

半城伤御伤魂 提交于 2019-12-05 19:39:24

"Documents library" capability in Visual Studio 2013 has been removed since it is only available for Windows Store Company accounts. Without this capability you will get "Access is denied".

For more information, read here: http://lunarfrog.com/blog/2013/07/05/documents-library-capability-winrt/

Like this, notice under 'Extensions' I specify the file type I want to access (.txt) and then under 'Capabilities' I have added 'documentsLibrary'.

Then to actually write or access a file from there, something like this.

 var file = await KnownFolders.DocumentsLibrary.CreateFileAsync("myFile.txt", CreationCollisionOption.ReplaceExisting);
                        await FileIO.WriteTextAsync(file, data);

<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest" xmlns:m2="http://schemas.microsoft.com/appx/2013/manifest">
  <Identity Name="testapp" Publisher="CN=test.test" Version="1.5.0.3" />
  <Properties>
    <DisplayName>test.MetroApp</DisplayName>
    <PublisherDisplayName>test.test</PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>
  <Prerequisites>
    <OSMinVersion>6.3.0</OSMinVersion>
    <OSMaxVersionTested>6.3.0</OSMaxVersionTested>
  </Prerequisites>
  <Resources>
    <Resource Language="x-generate" />
  </Resources>
  <Applications>
    <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="test.MetroApp.App">
      <m2:VisualElements DisplayName="test.MetroApp" Square150x150Logo="Assets\test.png" Square30x30Logo="Assets\SmallLogo.png" Description="test.MetroApp" ForegroundText="light" BackgroundColor="#464646">
        <m2:SplashScreen Image="Assets\test.scale-620.png" BackgroundColor="#464646" />
        <m2:InitialRotationPreference>
          <m2:Rotation Preference="landscape" />
        </m2:InitialRotationPreference>
      </m2:VisualElements>
      <Extensions>
        <Extension Category="windows.fileTypeAssociation">
          <FileTypeAssociation Name="1">
            <DisplayName>AccessTXT</DisplayName>
            <SupportedFileTypes>
              <FileType>.txt</FileType>
            </SupportedFileTypes>
          </FileTypeAssociation>
        </Extension>
      </Extensions>
    </Application>
  </Applications>
  <Capabilities>
    <Capability Name="internetClient" />
    <Capability Name="privateNetworkClientServer" />
    <Capability Name="musicLibrary" />
    <Capability Name="documentsLibrary" />
  </Capabilities>
</Package>

According to the docs "your app must use the File Type Association declaration in the app manifest file to explicitly declare what file types (extensions) will be accessed or created in the Documents library".

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