【问题标题】:Load File as storagefile from Local drive (resource)从本地驱动器(资源)加载文件作为存储文件
【发布时间】:2013-06-22 08:37:14
【问题描述】:

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

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

谢谢。

【问题讨论】:

  • 你面临什么问题,你的问题不清楚。你使用的是PickSingleFileAsync()FileOpenPicker 类吗?
  • @Xyroid 我不知道如何选择文件如果我知道文件路径,我的意思是这样 E:\E Books\Final code\file.zip 。我的问题是如何使用 Url 将文件作为 StorageFile 获取。我在 google 中尝试过,但没有得到相关的答案。我希望你在这里得到答案。

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


【解决方案1】:

WinRT 具有StorageFile 类的GetFileFromPathAsync() 方法,但您无法使用该方法打开任何文件。您唯一的选择是使用StorageItemMostRecentlyUsedList 类。这对于获取保存到most recently used files listfuture access list 的所有文件的令牌很有用。要保存从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
}

使用令牌检索文件

StorageItemMostRecentlyUsedList MRU = new StorageItemMostRecentlyUsedList();

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

更新

await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token);

【讨论】:

  • 感谢您的简洁解释,但我的意图与此不同。我正在使用PickMultipleFilesAsync(); 加载多个文件并在Gridview 中显示这些值,在这里,如果我使用Item_click 在GV 中选择一个项目,我将使用ClickedItem 获取该值。在这里,我只知道 FilePath 和文件名之类的值。希望你了解情况。为什么我不使用GetFileFromPathAsync()
  • GetFileFromPathAsync() 仅允许与已知文件夹(库)和应用程序的本地和临时文件夹相关的路径。可以将token绑定到gridview的item上(虽然不显示),在ItemClick事件时获取token,然后就可以打开文件了。
  • 这是 ItemClick ItemEpub.EpubItems output = e.ClickedItem as ItemEpub.EpubItems; EpubPathToken = output.mruToken; StorageItemMostRecentlyUsedList MRU = new StorageItemMostRecentlyUsedList(output); StorageFile Epubfile = await MRU.GetFileAsync(EpubPathToken); 中的代码块 我在 new StorageItemMostRecentlyUsedList(output); 中遇到错误 错误是:“Windows.Storage.AccessCache.StorageItemMostRecentlyUsedList”不包含接受 1 个参数的构造函数
  • 这是我的代码 [pastebin.com/Wsx1tRJa] 你能告诉我这个错误的任何想法吗,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多