我正在使用 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/