从本地驱动器(资源)将文件作为存储文件加载 [英] Load File as storagefile from Local drive (resource)

查看:82
本文介绍了从本地驱动器(资源)将文件作为存储文件加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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具有类StorageFileGetFileFromPathAsync()方法,但是无法使用该方法打开任何文件.您唯一的选择是使用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);

这篇关于从本地驱动器(资源)将文件作为存储文件加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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