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



我正在使用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类。这对于获取保存到最近使用的文件列表或未来访问列表中的所有文件的令牌非常有用。要保存从FileOpenPicker访问的令牌,需要使用StorageApplicationPermissions类。在这里我给你如何保存一个文件的令牌&如何检索&访问该文件。

保存令牌

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
}

使用令牌检索文件

StorageItemMostRcentlyUsedList MRU=新的StorageItemMostCentryUsedList();

StorageFile文件=等待MRU.GetFileAsync(令牌)

更新

await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token);

最新更新