Windows 10 通用应用程序文件/目录访问 [英] Windows 10 Universal App File/Directory Access

查看:33
本文介绍了Windows 10 通用应用程序文件/目录访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它从文件系统上的可配置位置读取 jpeg 和 pdf 文件.目前有一个在 WPF 中实现的运行版本,现在我正在尝试转向新的 Windows 通用应用程序.

I´m developing an app that is reading jpeg and pdf files from a configurable location on the filesystem. Currently there is a running version implemented in WPF and now I´m trying to move to the new Windows Universal apps.

以下代码适用于 WPF:

The following code works fine with WPF:

public IList<string> GetFilesByNumber(string path, string number)
    {
        if (string.IsNullOrWhiteSpace(path))
            throw new ArgumentNullException(nameof(path));

        if (string.IsNullOrWhiteSpace(number))
            throw new ArgumentNullException(nameof(number));

        if (!Directory.Exists(path))
            throw new DirectoryNotFoundException(path);

        var files = Directory.GetFiles(path, "*" + number + "*",
           SearchOption.AllDirectories);

        if (files == null || files.Length == 0)
            return null;
        return files;
    }

在使用通用应用程序时,我遇到了一些问题:

With using Universal Apps I ran into some problems:

  • Directory.Exists 不可用
  • 如何从应用存储空间之外的目录中读取数据?

要从应用程序存储之外的其他目录读取,我尝试了以下操作:

To read from an other directory outside the app storage I tried the following:

StorageFolder folder = StorageFolder.GetFolderFromPathAsync("D:\texts\");
var fileTypeFilter = new string[] { ".pdf", ".jpg" };
QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderBySearchRank, fileTypeFilter);
queryOptions.UserSearchFilter = "142";
StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(queryOptions);
IReadOnlyList<StorageFile> files = queryResult.GetFilesAsync().GetResults();

问题是:它不起作用,但我得到一个例外:

The thing is: It isn´t working, but I get an exception:

System.UnauthorizedAccessException"类型的异常发生在 TextManager.Universal.DataAccess.dll 中,但未在用户代码中处理附加信息:访问被拒绝.(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))

An exception of type 'System.UnauthorizedAccessException' occurred in TextManager.Universal.DataAccess.dll but was not handled in user code Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

我知道您必须在清单中配置一些权限,但我找不到适合文件系统 IO 操作的权限...

I know that you have to configure some permissions in the manifest, but I can´t find one suitable for filesystem IO operations...

是否有人也遇到过这样的问题/可能的解决方案?

Did someone also have such problems/a possible solution?

解决方案:从@Rico Suter 给我的解决方案中,我选择了 FutureAccessList 与 FolderPicker 相结合.也可以在程序重启后使用Token访问入口.

Solution: From the solutions that @Rico Suter gave me, I chosed the FutureAccessList in combination with the FolderPicker. It is also possible to access the entry with the Token after the program was restarted.

我还可以向您推荐用户体验指南和这个Github 示例.

I can also recommend you the UX Guidlines and this Github sample.

非常感谢!

推荐答案

在 UWP 应用中,您只能访问以下文件和文件夹:

In UWP apps, you can only access the following files and folders:

  • Directories which are declared in the manifest file (e.g. Documents, Pictures, Videos folder)
  • Directories and files which the user manually selected with the FileOpenPicker or FolderPicker
  • Files from the FutureAccessList or MostRecentlyUsedList
  • Files which are opened with a file extension association or via sharing

如果您需要访问 D: 中的所有文件,用户必须使用 FolderPicker 手动选择 D: 驱动器,然后您就可以访问其中的所有文件这个驱动器...

If you need access to all files in D:, the user must manually pick the D: drive using the FolderPicker, then you have access to everything in this drive...

更新:

Windows 10 build 17134(2018 年 4 月更新,版本 1803)为 UWP 应用添加了额外的文件系统访问功能:

Windows 10 build 17134 (2018 April Update, version 1803) added additional file system access capabilities for UWP apps:

  • 任何声明了 AppExecutionAlias 的 UWP 应用程序(常规窗口应用程序或控制台应用程序)现在都被授予对当前工作目录和向下的文件和文件夹的隐式访问权限,当它是从命令行激活的.当前工作目录来自用户选择执行 AppExecutionAlias 的任何文件系统位置.

  • Any UWP app (either a regular windowed app or a console app) that declares an AppExecutionAlias is now granted implicit access to the files and folders in the current working directory and downward, when it’s activated from a command line. The current working directory is from whatever file-system location the user chooses to execute your AppExecutionAlias.

新的 broadFileSystemAccess 功能授予应用与当前运行应用的用户相同的文件系统访问权限,而无需文件选择器样式提示.可以通过以下方式在清单中设置此访问权限:

The new broadFileSystemAccess capability grants apps the same access to the file system as the user who is currently running the app without file-picker style prompts. This access can be set in the manifest in the following manner:

    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    ...
    IgnorableNamespaces="uap mp uap5 rescap">
    ...
    <Capabilities>
      <rescap:Capability Name="broadFileSystemAccess" />
    </Capabilities>

在 MSDN 杂志文章中详细讨论了这些更改及其意图,标题为 Universal Windows Platform- 缩小 UWP-Win32 差距.文章指出以下几点:

These changes and their intention are discussed at length in the MSDN Magazine article titled Universal Windows Platform - Closing UWP-Win32 Gaps. The articles notes the following:

如果您声明任何受限能力,这会触发额外的在您向商店提交包裹时进行审查出版物....如果你有这个,你就不需要 AppExecutionAlias能力.因为这是一个如此强大的功能,微软将仅当应用程序开发人员提供令人信服的请求的原因,如何使用它的描述,以及解释这如何使用户受益.

If you declare any restricted capability, this triggers additional scrutiny at the time you submit your package to the Store for publication. ... You don’t need an AppExecutionAlias if you have this capability. Because this is such a powerful feature, Microsoft will grant the capability only if the app developer provides compelling reasons for the request, a description of how this will be used, and an explanation of how this benefits the user.

进一步:

如果您声明了 broadFileSystemAccess 功能,则不需要声明任何范围更窄的文件系统功能(文件、图片或视频);实际上,应用程序不得同时声明broadFileSystemAccess 和其他三个文件系统中的任何一个能力.

If you declare the broadFileSystemAccess capability, you don’t need to declare any of the more narrowly scoped file-system capabilities (Documents, Pictures or Videos); indeed, an app must not declare both broadFileSystemAccess and any of the other three file-system capabilities.

最后:

即使在应用被授予能力后,还有一个运行时检查,因为这构成了隐私问题用户.就像其他隐私问题一样,该应用程序会触发首次使用时的用户同意提示.如果用户选择拒绝许可,应用必须对此具有弹性.

Even after the app has been granted the capability, there’s also a runtime check, because this constitutes a privacy concern for the user. Just like other privacy issues, the app will trigger a user-consent prompt on first use. If the user chooses to deny permission, the app must be resilient to this.

这篇关于Windows 10 通用应用程序文件/目录访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆