当Documents文件夹位于OneDrive上的已知文件夹移动(KFM)下时,从KnownFolders.DocumentsLibrary读取文件失败(E_ACCESSDENIED)

问题描述 投票:0回答:1

我有一个应用程序从KnownFolders.DocumentsLibrary使用StorageFolder/StorageFile读取文件。在Onedrive接管Documents文件夹之前,我的应用程序正常运行。因此,声明了应用清单中的所有功能/文件类型关联。

我试图声明broadFileSystemAccess功能,但得到了相同的结果。

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

我希望像以前一样阅读文件,但现在我知道了

{System.UnauthorizedAccessException: (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) }
win-universal-app windows-10-universal
1个回答
0
投票

KnownFolders.DocumentsLibrary清楚地解释了它。

文档库不适用于一般用途。有关更多信息,请参阅App capability declarations

如果您的应用必须创建和更新只有您的应用使用的文件,请考虑使用应用的LocalCache文件夹。

或者,让用户使用文件选择器选择文件位置。有关更多信息,请参阅Open files and folders with a picker

因此,如果您没有特定要求,我建议您使用ApplicationData Folder或使用文件夹和文件选择器。

文档与其他应用程序共享,因此唯一的常见位置是文档。文件选择器不是一个选项。主要问题是One Drive接管文件夹,因为应用程序已经运行了几年。

如果必须使用DocumentsLibrary,则可以按照KnownFolders.DocumentsLibrary文档在清单文件中添加文档库功能,并注册至少一个文件类型关联声明。然后你可以从那里读取文件。

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>
© www.soinside.com 2019 - 2024. All rights reserved.