Search code examples
windows-runtimewindows-phone-8.1windows-dev-center

PickSaveFileAndContinue: UnauthorizedAccessException on Windows 10 mobile


I am using this code to let users to choose where to save a file:

        var fileSavePicker = new FileSavePicker();
        fileSavePicker.FileTypeChoices.Add("Pdf", new List<string>(){".pdf"});
        fileSavePicker.SuggestedFileName = $"{pdfFile.Name}";
        fileSavePicker.SuggestedSaveFile = pdfFile;
        fileSavePicker.PickSaveFileAndContinue();

This code works fine on Windows Phone 8.1 but give me an exception (System.UnauthorizedAccessException) when running on Windows 10 mobile. How can I solve this?


Solution

  • When I use FileSavePicker.PickSaveFileAndContinue method in the visual, there is an error “ The FileSavePicker.PickSaveFileAndContinue() is obsolete: instead, use PickSaveFileAsync() ”. So you can use FileSavePicker.PickSaveFileAsync() method in Windows 10 mobile.

    enter image description here

    Update: I have test Windows Phone 8.1 on Windows 10 Mobile, it was ok. The code of my project below , you can refer to.

    You can also refer to this sample about FileSavePicker.

     private void SaveFileButton_Click(object sender, RoutedEventArgs e)
        {
            // Clear previous returned file name, if it exists, between iterations of this scenario
            OutputTextBlock.Text = "";
    
            FileSavePicker savePicker = new FileSavePicker();
            savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            // Dropdown of file types the user can save the file as
            savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
            // Default file name if the user does not type one in or select a file to replace
            savePicker.SuggestedFileName = "New Document";
    
            savePicker.PickSaveFileAndContinue();
        }
    
        /// <summary>
        /// Handle the returned file from file picker
        /// This method is triggered by ContinuationManager based on ActivationKind
        /// </summary>
        /// <param name="args">File save picker continuation activation argment. It cantains the file user selected with file save picker </param>
        public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
        {
            StorageFile file = args.File;
            if (file != null)
            {
                // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
                CachedFileManager.DeferUpdates(file);
                // write to file
                await FileIO.WriteTextAsync(file, file.Name);
                // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
                // Completing updates may require Windows to ask for user input.
                FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
                if (status == FileUpdateStatus.Complete)
                {
                    OutputTextBlock.Text = "File " + file.Name + " was saved.";
                }
                else
                {
                    OutputTextBlock.Text = "File " + file.Name + " couldn't be saved.";
                }
            }
            else
            {
                OutputTextBlock.Text = "Operation cancelled.";
            }
        }