无水印.将文件从 FileOpenPicker 复制到本地存储 [英] UWP . Copy file from FileOpenPicker to localstorage

查看:24
本文介绍了无水印.将文件从 FileOpenPicker 复制到本地存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FileOpenPicker picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
picker.FileTypeFilter.Add(".txt");

用户选择要打开的文件.我如何将该文件存储/复制/保存到本地存储以备将来使用,以便每次打开应用时,它都会自动选择该文件?

A user chooses a file to open. How can I store/copy/save that file to localstorage for future use, so every time the app opens, it picks automatically that file?

推荐答案

在用户使用 FileOpenPicker 打开文件后,您可以使用 StorageApplicationPermissions 缓存"对其的访问API.

After the user opens the file using the FileOpenPicker you can "cache" access to it using StorageApplicationPermissions API.

一旦您拥有要自动打开的 StorageFile,您可以使用以下代码缓存"您对它的访问:

Once you have the StorageFile you want to open automatically, you can "cache" your access to it using the following code:

string token = StorageApplicationPermissions.FutureAccessList.Add( file );

您返回的是一个字符串令牌,您可以将其保存在例如应用程序设置中.下次打开应用程序时,您可以使用以下代码再次检索文件:

What you get back is a string token, which you can save for example in the app settings. Next time the app is opened, you can retrieve the file again using the following code:

StorageFile file = 
   await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);

请注意,此 API 最多可存储 1000 个项目,因此如果您希望可以添加更多项目,则必须确保删除旧文件,否则您将无法添加新文件.

Note that this API has limitation of at most 1000 items stored, so if you expect that more could be added, you will have to ensure the older files are removed otherwise you would not be able to add new files.

还有一个替代方案 - StorageApplicationPermissions.MostRecentlyUsedList,您可以使用与 FutureAccessList 相同的方式,但它具有自动管理列表的优点.它最多可以存储 25 个项目,但可以在不再需要时自动删除最旧的项目.

There is also alternative - StorageApplicationPermissions.MostRecentlyUsedList which you can use the same way as the FutureAccessList, but it has the advantage of automatically managing the list. It can store up to 25 items, but it is able to automatically remove the oldest ones when not needed anymore.

另请注意,此 API 不仅可以缓存对文件的访问,还可以缓存对文件夹的访问 (StorageFolder).

Also note, that this APIs can cache access not only to files but also to folders (StorageFolder).

如果您只想创建所选文件的本地副本,您可以将其复制到应用程序的本地文件夹中.

If you just want to create a local copy of the picked file, you can copy it to the local folder of the app.

var file = await picker.PickSingleFileAsync();
if ( file != null )
{
   await file.CopyAsync( ApplicationData.Current.LocalFolder );
}

这篇关于无水印.将文件从 FileOpenPicker 复制到本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆