从本地驱动器(资源)将文件作为存储文件加载

编程入门 行业动态 更新时间:2024-10-27 22:21:29
本文介绍了从本地驱动器(资源)将文件作为存储文件加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用C#开发Windows 8应用程序. 在这里,我使用FilePicker从所需的位置选择文件, 我知道我从本地驱动器中选择的文件的路径.

I am developing Windows 8 app using C#. Here I am picking the file from my desired Location Using FilePicker, I Know the path of file which I picked from Local drive.

我想将该文件用作存储文件.

I want to use the File as a Storage file.

StorageFile Newfile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(Path); // Path is file path StorageFile file = await KnownFolders.PicturesLibrary.GetFileAsync(Path);

但这仅适用于我的项目所在的&另一个用于从图片库加载文件. 谁能给我正确的方法.

But this is for only where my project located & another one for Load file from Pictures Library. Can any one give me the right Way.

谢谢.

推荐答案

WinRT具有类StorageFile的GetFileFromPathAsync()方法,但是无法使用该方法打开任何文件.您唯一的选择是使用StorageItemMostRecentlyUsedList类.这对于获取保存到最近使用的文件列表或未来的访问列表.要保存从FileOpenPicker访问的令牌,您需要使用StorageApplicationPermissions类.在这里,我为您介绍如何为文件&保存令牌.如何为&获取令牌访问该文件.

WinRT has GetFileFromPathAsync() method of class StorageFile, but you can not open any file with that method. Only option you have is to use StorageItemMostRecentlyUsedList class. Which is useful to get the token for all the files that was saved to either most recently used files list or future access list. To save token for the which was accessed from FileOpenPicker, you need to use StorageApplicationPermissions class. Here I'm giving you how to save token for a file & how to retrieve token for & access that file.

要保存令牌

FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.Thumbnail; openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".png"); StorageFile file = await openPicker.PickSingleFileAsync(); if (file != null) { // Add to most recently used list with metadata (For example, a string that represents the date) string mruToken = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "20130622"); // Add to future access list without metadata string faToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file); } else { // The file picker was dismissed with no file selected to save }

使用令牌检索文件

StorageItemMostRecentlyUsedList MRU =新的StorageItemMostRecentlyUsedList();

StorageItemMostRecentlyUsedList MRU = new StorageItemMostRecentlyUsedList();

StorageFile文件=等待MRU.GetFileAsync(token);

StorageFile file = await MRU.GetFileAsync(token);

更新

await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token);

更多推荐

从本地驱动器(资源)将文件作为存储文件加载

本文发布于:2023-11-24 05:49:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624156.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   驱动器   加载   资源

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!