Can I remember an opened file in a UWP app?

不想你离开。 提交于 2020-08-22 06:35:29

问题


I'm using a FilePicker in my app to have the user select a file. But next time the app runs the user has to open it again that way. I want to offer the possibility to open the recent files. Is that possible?


回答1:


You can do that using the FutureAccessList class. That is a mechanism to remember opened files and/or folders. You have to assign a token yourself to make it unique in your app, but you can use a Guid.NewGuid().ToString() to make it unique.

To remember a file, you can use a method like this:

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);
}

You can use this mechanism to store a list of tokens with the filename. This way you can provide the user a clue of the files and have a way to open it again.




回答2:


You won't have access rights to a brokered file directly from a path, so there's little value in saving it. FileOpenPicker is what gives you the access, and it's limited to the specific file it returns. All your app can explicitly is the SuggestedStartLocation property on the FileOpenPicker.

The app can make use of Windows.Storage.AccessCache to remember the items you had been given access to by the FileOpenPicker. In particular see StorageItemMostRecentlyUsedList.

See Skip the path: stick to the StorageFile



来源:https://stackoverflow.com/questions/38103655/can-i-remember-an-opened-file-in-a-uwp-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!