UWP . Copy file from FileOpenPicker to localstorage

心不动则不痛 提交于 2021-01-21 09:51:10

问题


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?


回答1:


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

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

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.

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.

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

Copying the file to AppData folder

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



回答2:


StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
    var yourPath = file.Path;
}

but It won't work as you expect. But remember you can't open file from location you (your app) don't have access to.

edit: yeah, I see in comments that I have missed some part of the qestion ;) the easiest way to store the information for future re-use would be propably to use LocalSettings https://msdn.microsoft.com/library/windows/apps/windows.storage.applicationdata.localsettings.aspx (sorry for the link, but there is no use in copying info from there)




回答3:


You could:

1) Store the file name in your project settings;

YourNameSpace.Properties.Settings.fileToLoad;

2) write the file name in a local file (look at TextWriter namespace);

3) store the file name in your database if your application is data-driven

... and others.

I am presuming here that you're using WinForms or Console app. If you are using a webForm, you would need to store the file name in a cookie so you could attach the right file to the right user before they log in or give you credenstials. For Webforms, then, look into the use of cookies.




回答4:


Just to add to the above suggestions, following example from Official Microsoft document shows how to Store file for future access:

var openPicker = new FileOpenPicker();
StorageFile file = await openPicker.PickSingleFileAsync();
// Process picked file
if (file != null)
{
    // Store file for future access
    Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
}
else
{
    // The user didn't pick a file
}


来源:https://stackoverflow.com/questions/39111925/uwp-copy-file-from-fileopenpicker-to-localstorage

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