none
怎样使用文件选取器选取一个文件夹(比如说下载文件夹)后,保存状态,让下次打开软件仍有权限访问该文件夹 RRS feed

  • 问题

  • 如题,在做一个类似文件管理器的软件,但是wp8.1默认只能访问图片库,音乐库,视频库这些文件夹,访问下载等文件夹只能通过FolderPicker,怎样做能使选取后,下次打开软件仍能访问?使用序列化保存数据可以吗?求解
    2014年10月24日 11:40

答案

  • 你好,

    你的需求在官方的sample中有部分实现:https://code.msdn.microsoft.com/windowsapps/File-picker-sample-9f294cba

    在其中第三个案例中,使用到了ContinuationManager 帮助类,它可以记住在进行下一步时,用户的操作和完成的任务。

    然后使用 FolderPickerContinuationEventArgs.Folder 属性就可以记录文件夹:
    http://msdn.microsoft.com/en-us/library/windows.applicationmodel.activation.folderpickercontinuationeventargs.folder.aspx

    private void PickFolderButton_Click(object sender, RoutedEventArgs e)
    {
                // Clear previous returned folder name, if it exists, between iterations of this scenario
                OutputTextBlock.Text = "";
    
                FolderPicker folderPicker = new FolderPicker();
                folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
                folderPicker.FileTypeFilter.Add(".docx");
                folderPicker.FileTypeFilter.Add(".xlsx");
                folderPicker.FileTypeFilter.Add(".pptx");
    
                folderPicker.PickFolderAndContinue();
    }
    
    /// <summary>
    /// Handle the returned folder from folder picker
    /// This method is triggered by ContinuationManager based on ActivationKind
    /// </summary>
    /// <param name="args">Folder picker continuation activation argment. It cantains the folder user selected with folder picker </param>
    public void ContinueFolderPicker(FolderPickerContinuationEventArgs args)
    {
                StorageFolder folder = args.Folder;
                if (folder != null)
                {
                    // Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
                    // 应用现在对于所选文件夹内的文件均获得读写权限(包括子文件夹)
                    StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
                    OutputTextBlock.Text = "Picked folder: " + folder.Name;
                }
                else
                {
                    OutputTextBlock.Text = "Operation cancelled.";
                }
    }

    当应用加载的时候,只需要获得FutureAccessList中的数据,即可实现你要的效果,可参考以下文档:

    #How to track recently used files and folders
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh972344(v=win.10).aspx

    String mruFirstToken = StorageApplicationPermissions.MostRecentlyUsedList.Entries.First.Token;
    StorageFile retrievedFile = await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(mruFirstToken);


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年10月27日 8:35
    版主