怎么把UWP StorageFile转换成.NET FileInfo? [英] How to convert UWP StorageFile to .NET FileInfo?

查看:137
本文介绍了怎么把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 到Fileinfo?

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

天真的方法(获取StorageItem的完整路径并使用它构造Fileinfo)在尝试打开生成的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天全站免登陆