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

Kum*_*mar 3 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);
Run Code Online (Sandbox Code Playgroud)

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

谢谢。

Xyr*_*oid 5

WinRT具有GetFileFromPathAsync()class方法StorageFile,但是您不能使用该方法打开任何文件。您唯一的选择是使用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
}
Run Code Online (Sandbox Code Playgroud)

使用令牌检索文件

StorageItemMostRecentlyUsedList MRU =新的StorageItemMostRecentlyUsedList();

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

更新

await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token);
Run Code Online (Sandbox Code Playgroud)