代码之家  ›  专栏  ›  技术社区  ›  Peter Torr

如何访问用户在我的UWP应用程序的上一个会话中选择的文件或文件夹?

uwp
  •  0
  • Peter Torr  · 技术社区  · 6 年前

    我的应用程序要求用户选择一个文件(或整个文件夹),我将从中加载内容。我可以使用 FileOpenPicker FolderPicker 这方面的课程,而且效果很好。问题是,如果用户关闭我的应用程序(或者它被挂起并终止),我将无法访问 StorageFile StorageFolder 那是拾荒者送回来的。我不想让用户再次选择相同的文件或文件夹(那会很烦人)。

    Path 然后使用 broadFilesystemAccess 以后访问文件或文件夹的能力,但这是Microsoft必须批准的受限能力。有没有更好的方法重新打开用户以前在我的应用程序中打开的文件或文件夹?

    1 回复  |  直到 6 年前
        1
  •  5
  •   Peter Torr    6 年前

    这个 broadFilesystemAccess 能力是 principle of least privilege 也就是说你不应该要求 当您只需要访问 文件,这是一种用户可以随时关闭的功能,这意味着您将失去对文件/文件夹的访问权限。

    相反,您应该使用 FutureAccessList FileSavePicker 或通过 File 激活(例如,当用户在Windows资源管理器中双击一个文件时)或通过任何其他机制提供磁盘备份 IStorageItem

    IStorageItem项目

    // After user has picked file or folder, get the token and then
    // store it in your local settings however you want.
    var token = StorageApplicationPermissions.FutureAccessList.Add(file);
    SaveLastFileUserWasWorkingOnToSettings(token);
    
    // -----
    
    // When your app is launched again, look up the last-opened file in 
    // settings and then try to retrieve it.
    var token = GetLastFileUserWasWorkingOnFromSettings();
    if (StorageApplicationPermissions.FutureAccessList.ContainsItem(token))
    {
      var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);
      // use the file...
    }