gpt4 book ai didi

c# - Windows 10 通用应用程序文件/目录访问

转载 作者:IT王子 更新时间:2023-10-29 04:29:04 25 4
gpt4 key购买 nike

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

以下代码适用于 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;
}

在使用 Universal Apps 时我遇到了一些问题:

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

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

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();

问题是:它不工作,但我得到一个异常(exception):

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))

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

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

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

我还可以向您推荐 UX Guidlines还有这个Github sample .

非常感谢!

最佳答案

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

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

更新:

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

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

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

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

这些更改及其意图在标题为 Universal Windows Platform - Closing UWP-Win32 Gaps 的 MSDN 杂志文章中进行了详细讨论。 .文章指出以下内容:

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.

进一步:

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.

关于c# - Windows 10 通用应用程序文件/目录访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33082835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com