如何将 UWP StorageFile 转换为 .NET FileInfo? [英] How to convert UWP StorageFile to .NET FileInfo?

查看:16
本文介绍了如何将 UWP StorageFile 转换为 .NET FileInfo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在 UWP 支持 .NET Standard 2.0,它可以访问更多 System.IO 命名空间,包括 文件信息目录信息.

Now that UWP supports .NET Standard 2.0, it has access to more of the System.IO namespace, including Fileinfo and DirectoryInfo.

如何转换 UWP StorageFile到文件信息?还有一个 StorageFolder 到 DirectoryInfo 吗?

How does one convert an UWP StorageFile to a Fileinfo? And a StorageFolder to a DirectoryInfo for that matter?

尝试打开生成的 Fileinfo 时,天真的方法(获取 StorageItem 的完整路径并使用它构建 Fileinfo)失败,我真的很想摆脱使用 PCLStorage 如果可能.

The naïve approach (get the StorageItem's full path and construct a Fileinfo using it) fails when trying to open the resulting Fileinfo, and I'd really like to get away from using PCLStorage if possible.

推荐答案

System.IO.File 和 System.IO.FileInfo 早已可用于 UWP 应用.它们在 .Net Standard 2.0 中并不新鲜

System.IO.File and System.IO.FileInfo have long been available to UWP apps. They aren't new with .Net Standard 2.0

StorageFile 和 FileInfo 不做同样的事情,通常不能互换.您没有说明遇到了什么错误,但我的猜测是它是 AccessDenied,因为您的应用无法直接访问您尝试使用的路径.

StorageFile and FileInfo don't do the same thing and aren't generally interchangeable. You don't say what error you're getting, but my guess is that it is AccessDenied because your app doesn't have direct access to the path you are trying to use.

默认情况下,应用程序只能直接访问它们的 ApplicationData 和 InstalledLocations.他们可以使用 System.IO 直接访问这些位置.要访问已被用户授予访问权限的其他位置(直接通过文件选择器,通过包功能隐式等),应用程序需要通过 StorageFile 和 StorageFolder 类通过文件代理.

By default apps have direct file access only to their ApplicationData and InstalledLocations. They can use System.IO to access those locations directly. To access other locations that have been granted access by the user (directly through a File picker, implicitly by package capabilities, etc.) the app needs to go through the file broker via the StorageFile and StorageFolder classes.

您可以从 IStorageHandleAccess 并从中初始化 System.IO.File,但 FileInfo 不提供从 HANDLE 构造的方法.

You can get a brokered Win32 file HANDLE from IStorageHandleAccess and initialize a System.IO.File from it, but FileInfo doesn't provide a way to construct from a HANDLE.

这篇关于如何将 UWP StorageFile 转换为 .NET FileInfo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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