【问题标题】:Can I remember an opened file in a UWP app?我可以记住 UWP 应用中打开的文件吗?
【发布时间】:2016-11-01 09:37:28
【问题描述】:

我在我的应用程序中使用 FilePicker 让用户选择文件。但是下次应用程序运行时,用户必须以这种方式再次打开它。我想提供打开最近文件的可能性。这可能吗?

【问题讨论】:

  • 当然。将文件的路径保存在某处,例如在应用程序的设置文件中。处理失败,以防文件被移动、删除或无法访问。
  • 请查看如何在stackoverflow上提出好问题stackoverflow.com/help/how-to-ask
  • 见我的answer in another thread。第一次请求权限时,它会记住应用程序生命周期的文件夹和/或文件。

标签: uwp


【解决方案1】:

您可以使用FutureAccessList 类来做到这一点。这是一种记住打开的文件和/或文件夹的机制。您必须自己分配一个令牌以使其在您的应用程序中唯一,但您可以使用 Guid.NewGuid().ToString() 使其唯一。

要记住一个文件,你可以使用这样的方法:

public string RememberFile(StorageFolder file)
{
    string token = Guid.NewGuid().ToString();
    StorageApplicationPermissions.FutureAccessList.AddOrReplace(token, file);
    return token;
}
To retrieve the file the next time, you can use this:

public async Task<StorageFile> GetFileForToken(string token)
{
    if (!StorageApplicationPermissions.FutureAccessList.ContainsItem(token)) return null;
    return await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);
}
To forget a token, you can use this:

public async Task<StorageFile> GetFileForToken(string token)
{
    if (!StorageApplicationPermissions.FutureAccessList.ContainsItem(token)) return null;
    return await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);
}

您可以使用此机制来存储带有文件名的令牌列表。这样您就可以向用户提供文件的线索,并有办法再次打开它。

【讨论】:

    【解决方案2】:

    您没有直接从路径访问代理文件的权限,因此保存它没有什么价值。 FileOpenPicker 为您提供访问权限,并且仅限于它返回的特定文件。您的应用程序可以明确地是 FileOpenPicker 上的 SuggestedStartLocation 属性。

    该应用程序可以使用Windows.Storage.AccessCache 来记住 FileOpenPicker 授予您访问权限的项目。具体见StorageItemMostRecentlyUsedList

    Skip the path: stick to the StorageFile

    【讨论】:

      猜你喜欢
      • 2019-02-01
      • 1970-01-01
      • 2016-08-23
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 2021-12-29
      相关资源
      最近更新 更多