How to convert UWP StorageFile to .NET FileInfo?

喜欢而已 提交于 2019-12-06 09:40:31

问题


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

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

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.


回答1:


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

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.

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.

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.



来源:https://stackoverflow.com/questions/47017495/how-to-convert-uwp-storagefile-to-net-fileinfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!