Reading files from KnownFolders.DocumentsLibrary fails (E_ACCESSDENIED) when Documents folder is under Known Folder Move (KFM) on OneDrive

0

I have an app reading files from KnownFolders.DocumentsLibrary using StorageFolder/StorageFile. Before Onedrive takes over the Documents folder my app was working ok. So all Capabilities / File Type Associations in the app manifest are declared.

I've tried to declare broadFileSystemAccess capability but got the same result.

StorageFolder folder = Windows.Storage.KnownFolders.DocumentsLibrary;
string content = await FileIO.ReadTextAsync("file.txt", 
Windows.Storage.Streams.UnicodeEncoding.Utf8);

I expect to read the file as before but now I get

{System.UnauthorizedAccessException: (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) }
win-universal-app
windows-10-universal
asked on Stack Overflow Mar 28, 2019 by Pedro Garcia • edited Mar 29, 2019 by shazyriver

1 Answer

0

The KnownFolders.DocumentsLibrary has explained it clearly.

The Documents library is not intended for general use. For more info, see App capability declarations.

If your app has to create and update files that only your app uses, consider using the app's LocalCache folder.

Alternatively, let the user select the file location by using a file picker. For more info, see Open files and folders with a picker.

So, if you have no specific requirement, I suggested you use ApplicationData Folder or use Folder and files picker.

The Documents are shared with other apps, so the only common location is Documents. The file picker is not an option. The main problem comes with the One Drive taking over the folder, as the App have been working ok for a couple of years.

If you have to use DocumentsLibrary, you could follow the KnownFolders.DocumentsLibrary document to add Documents Library capability in manifest file and register at least one File Type Association declaration. Then you could read the file from there.

StorageFolder folder = Windows.Storage.KnownFolders.DocumentsLibrary;
var files = await folder.GetFilesAsync();
foreach (var f in files)
{
     string text = await Windows.Storage.FileIO.ReadTextAsync(f);
     Debug.WriteLine(f.DisplayName + ": " + text);
}
<Extensions>
    <uap:Extension Category="windows.fileTypeAssociation">
      <uap:FileTypeAssociation Name="appdoc">
        <uap:SupportedFileTypes>
          <uap:FileType>.txt</uap:FileType>
        </uap:SupportedFileTypes>
      </uap:FileTypeAssociation>
    </uap:Extension>
  </Extensions>
 ....
 <Capabilities>
    <Capability Name="internetClient" />
    <uap:Capability Name="documentsLibrary" />
 </Capabilities>
answered on Stack Overflow Mar 29, 2019 by Xie Steven • edited Apr 1, 2019 by Xie Steven

User contributions licensed under CC BY-SA 3.0