How can I save a StorageFile to use later?

旧城冷巷雨未停 提交于 2019-12-24 06:52:11

问题


I am working in WinRT, and I am kind of stuck:

I am writing a music player with a media library capability. I keep information about the music (such as artists etc) in a SQLite database. I wanted to let the user keep his music anywhere he wants to, instead of the windows way, where it all has to be in the 'Music' library.

Users can add the music inside folders using a folder picker. The problem I have is this: how can I access these files later, e.g after the application restarts?

Keeping the path doesn't work, since I always get "Access Denied" errors. The only time I can access the files is using the StorageFile objects I get from browsing the folder.

How can I solve this issue?


回答1:


As in the comments already given, the Windows.Storage.AccessCache (http://msdn.microsoft.com/en-us/library/windows/apps/br230566.aspx) is the API you need to use for this. However, instead of saving access to each individual StorageFile, use the folder picker and save permissions for the StorageFolder object instead (the API works for both). It's unlikely that you'll hit the 1000 item limit for folders.




回答2:


Windows Runtime apps are sandboxed. If you want to access arbitrary folder locations, you have to use the file picker.




回答3:


to access files in future

        string key = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(storageFile);
        //save this key for access file later

        //Access file from saved key
        StorageFile file = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(key);

if you have too much file you can add StorageFolder of files

        string key = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(storageFolder);
        StorageFolder folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(key);

then get StorageFiles from the parent StorageFolder

StorageFile childFile = await folder.GetFileAsync("filename");


来源:https://stackoverflow.com/questions/13518649/how-can-i-save-a-storagefile-to-use-later

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