Windows 10 个通用应用 File/Directory 访问权限
Windows 10 Universal App File/Directory Access
我正在开发一个从文件系统上的可配置位置读取 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();
问题是:它不工作,但我得到一个例外:
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 操作的...
有人也有这样的problems/a可能的解决方案吗?
解法:
从@Rico Suter 给我的解决方案中,我选择了 FutureAccessList 结合 FolderPicker。也可以在程序重启后用Token访问入口
我还可以向您推荐UX Guidlines and this Github sample。
非常感谢!
在 UWP 应用程序中,您只能访问以下文件和文件夹:
- 在 manifest file 中声明的目录(例如文档、图片、视频文件夹)
- 用户使用 FileOpenPicker or FolderPicker
手动选择的目录和文件
- 文件来自 FutureAccessList or MostRecentlyUsedList
- 使用文件扩展名关联或通过共享打开的文件
如果您需要访问 D:\
中的所有文件,用户必须使用 FolderPicker 手动选择 D:\
驱动器,然后您可以访问该驱动器中的所有文件...
更新:
Windows 10 build 17134(2018 年四月更新,版本 1803)为 UWP 应用添加了额外的文件系统访问功能:
任何声明 AppExecutionAlias
的 UWP 应用程序(常规窗口应用程序或控制台应用程序)现在都被授予对当前工作目录及以下目录中文件和文件夹的隐式访问权限,当它从命令行激活时。当前工作目录来自用户选择执行 AppExecutionAlias 的任何 file-system 位置。
新的 broadFileSystemAccess
功能授予应用程序与当前 运行 没有 file-picker 样式提示的应用程序的用户相同的文件系统访问权限。可以通过以下方式在清单中设置此访问权限:
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.
根据 MSDN 文档:"The file picker allows an app to access files and folders, to attach files and folders, to open a file, and to save a file."
https://msdn.microsoft.com/en-us/library/windows/apps/hh465182.aspx
您可以通过标准用户界面使用文件选择器读取文件。
此致
这不是真的:
使用文件扩展名关联或通过共享打开的文件
尝试一下,通过从邮件(outlook)或桌面打开文件...
它根本不起作用
您首先必须通过文件选择器授予权限。
所以这是...
接受的答案不再完整。现在可以在应用程序清单中声明 broadFileSystemAccess
以任意读取文件系统。
File Access Permissions 页面有详细信息。
请注意,用户仍然可以通过设置应用撤销此权限。
您可以在 VS 2017 中从 UI 开始。
单击清单文件 -> 功能 -> 检查照片库或任何你想要的东西。
这是一项受限的能力。可在 设置 > 隐私 > 文件系统中配置访问权限。 并为您的应用程序启用访问权限。由于用户可以随时在“设置”中授予或拒绝权限,因此您应该确保您的应用能够适应这些更改。如果您发现您的应用程序没有访问权限,您可以选择通过向 Windows 10 文件系统访问和隐私文章提供 link 来提示用户更改设置。请注意,用户必须关闭应用程序、切换设置并重新启动应用程序。如果他们在应用程序 运行 时切换设置,平台将暂停您的应用程序以便您可以保存状态,然后强行终止应用程序以应用新设置。在 2018 年 4 月的更新中,权限的默认设置为开启。在 2018 年 10 月更新中,默认为关闭。
我正在开发一个从文件系统上的可配置位置读取 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();
问题是:它不工作,但我得到一个例外:
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 操作的...
有人也有这样的problems/a可能的解决方案吗?
解法: 从@Rico Suter 给我的解决方案中,我选择了 FutureAccessList 结合 FolderPicker。也可以在程序重启后用Token访问入口
我还可以向您推荐UX Guidlines and this Github sample。
非常感谢!
在 UWP 应用程序中,您只能访问以下文件和文件夹:
- 在 manifest file 中声明的目录(例如文档、图片、视频文件夹)
- 用户使用 FileOpenPicker or FolderPicker 手动选择的目录和文件
- 文件来自 FutureAccessList or MostRecentlyUsedList
- 使用文件扩展名关联或通过共享打开的文件
如果您需要访问 D:\
中的所有文件,用户必须使用 FolderPicker 手动选择 D:\
驱动器,然后您可以访问该驱动器中的所有文件...
更新:
Windows 10 build 17134(2018 年四月更新,版本 1803)为 UWP 应用添加了额外的文件系统访问功能:
任何声明
AppExecutionAlias
的 UWP 应用程序(常规窗口应用程序或控制台应用程序)现在都被授予对当前工作目录及以下目录中文件和文件夹的隐式访问权限,当它从命令行激活时。当前工作目录来自用户选择执行 AppExecutionAlias 的任何 file-system 位置。新的
broadFileSystemAccess
功能授予应用程序与当前 运行 没有 file-picker 样式提示的应用程序的用户相同的文件系统访问权限。可以通过以下方式在清单中设置此访问权限:
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 bothbroadFileSystemAccess
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.
根据 MSDN 文档:"The file picker allows an app to access files and folders, to attach files and folders, to open a file, and to save a file."
https://msdn.microsoft.com/en-us/library/windows/apps/hh465182.aspx
您可以通过标准用户界面使用文件选择器读取文件。
此致
这不是真的: 使用文件扩展名关联或通过共享打开的文件 尝试一下,通过从邮件(outlook)或桌面打开文件... 它根本不起作用 您首先必须通过文件选择器授予权限。 所以这是...
接受的答案不再完整。现在可以在应用程序清单中声明 broadFileSystemAccess
以任意读取文件系统。
File Access Permissions 页面有详细信息。
请注意,用户仍然可以通过设置应用撤销此权限。
您可以在 VS 2017 中从 UI 开始。
单击清单文件 -> 功能 -> 检查照片库或任何你想要的东西。
这是一项受限的能力。可在 设置 > 隐私 > 文件系统中配置访问权限。 并为您的应用程序启用访问权限。由于用户可以随时在“设置”中授予或拒绝权限,因此您应该确保您的应用能够适应这些更改。如果您发现您的应用程序没有访问权限,您可以选择通过向 Windows 10 文件系统访问和隐私文章提供 link 来提示用户更改设置。请注意,用户必须关闭应用程序、切换设置并重新启动应用程序。如果他们在应用程序 运行 时切换设置,平台将暂停您的应用程序以便您可以保存状态,然后强行终止应用程序以应用新设置。在 2018 年 4 月的更新中,权限的默认设置为开启。在 2018 年 10 月更新中,默认为关闭。