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

标签 c# microsoft-metro windows-store-apps

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

我想将文件用作存储文件。

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

  StorageFile file = await KnownFolders.PicturesLibrary.GetFileAsync(Path);

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

谢谢。

最佳答案

WinRT 具有 StorageFile 类的 GetFileFromPathAsync() 方法,但您无法使用该方法打开任何文件。您唯一的选择是使用 StorageItemMostRecentlyUsedList 类。这对于获取保存到 most recently used files list 中的所有文件的 token 很有用。或 future access list .要保存从 FileOpenPicker 访问的 token ,您需要使用 StorageApplicationPermissions 类。在这里,我将向您介绍如何保存文件的 token 以及如何检索和访问该文件的 token 。

保存 token

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
}

使用 token 检索文件

StorageItemMostRecentlyUsedList MRU = new StorageItemMostRecentlyUsedList();

StorageFile file = await MRU.GetFileAsync(token);

更新

await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token);

关于c# - 从本地驱动器(资源)加载文件作为存储文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17248849/

相关文章:

c# - 使用Icomparer的CustomSort字符串

c# - 在 Windows Vista 和 7 中,我无法访问 %DEFAULTUSERPROFILE% 系统变量 - 它显示为未找到

c# - 如何在 tableLayoutPanel 中设置 columnspan

windows-8 - 如何在 Windows 8 METRO 应用程序中实现错误日志记录?

managed - Metro 风格应用程序是托管的还是 native 的?如果是 native 的,它们如何在 arm 和 x86 上运行?

windows - 如何在 Windows 8 上从 PowerShell 运行 Metro-App?

c# - Windows Phone 8.1 MapIcon 单击事件

c# - 在属性访问器中调用等待方法 [Windows 应用商店应用/Metro 应用]

c# - OAuth 2.0 与 Web API 和 Xamarin

xaml - Windows Phone 8.1 上使用 MVVM Light 的双向数据绑定(bind)