I find it difficult to understand what is the cause of this error. I added FilePicker
in the manifest, and it doesn't look like I'm trying to do something crazy; just trying to save to a subfolder in the Documents folder ...
Error: "Unhandled exception of type" System.UnauthorizedAccessException "occurred in mscorlib.dll
Additional Information: Access denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) "
I have confirmed that my user account is an administrator and that she has full control over folders and files. But I'm not sure what else I can try.
public void NewBTN_Click(object sender, RoutedEventArgs e) { var mbox = new MessageDialog("Would you like to save changes before creating a new Note?", "Note+ Confirmation"); UICommand YesBTN = new UICommand("Yes", new UICommandInvokedHandler(OnYesBTN)); UICommand NoBTN = new UICommand("No", new UICommandInvokedHandler(OnNoBTN)); mbox.Commands.Add(YesBTN); mbox.Commands.Add(NoBTN); mbox.DefaultCommandIndex = 1; mbox.ShowAsync().Start(); } async void OnYesBTN(object command) { this.Dispatcher.Invoke(Windows.UI.Core.CoreDispatcherPriority.Normal, (s, a) => { // User clicked yes. Show File picker. HasPickedFile = true; }, this, null); if (HasPickedFile) { FileSavePicker savePicker = new FileSavePicker(); savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; // Dropdown of file types the user can save the file as savePicker.FileTypeChoices.Add("Cascading Stylesheet", new List<string>() { ".css" }); savePicker.FileTypeChoices.Add("Hypertext Markup Language", new List<string>() { ".html" }); savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" }); // Default extension if the user does not select a choice explicitly from the dropdown savePicker.DefaultFileExtension = ".txt"; // Default file name if the user does not type one in or select a file to replace savePicker.SuggestedFileName = "New Note"; StorageFile savedItem = await savePicker.PickSaveFileAsync(); if (null != savedItem) { // Application now has read/write access to the saved file StorageFolder sFolder = await StorageFolder.GetFolderFromPathAsync(savedItem.Path); try { StorageFile sFile = await sFolder.GetFileAsync(savedItem.FileName); IRandomAccessStream writeStream = await sFile.OpenAsync(FileAccessMode.ReadWrite); IOutputStream oStream = writeStream.GetOutputStreamAt(0); DataWriter dWriter = new DataWriter(oStream); dWriter.WriteString(Note.Text); await dWriter.StoreAsync(); oStream.FlushAsync().Start(); // Should've successfully written to the file that Windows FileSavePicker had created. } catch { var mbox = new MessageDialog("This file does not exist.", "Note+ Confirmation"); UICommand OkBTN = new UICommand("Ok", new UICommandInvokedHandler(OnOkBTN)); mbox.Commands.Add(OkBTN); mbox.DefaultCommandIndex = 1; mbox.ShowAsync().Start(); } } } } public void OnOkBTN(object command) { this.Dispatcher.Invoke(Windows.UI.Core.CoreDispatcherPriority.Normal, (s, a) => { // Do something here. }, this, null); } public void OnNoBTN(object command) { this.Dispatcher.Invoke(Windows.UI.Core.CoreDispatcherPriority.Normal, (s, a) => { // Don't save changes. Just create a new blank Note. Note.Text = String.Empty; }, this, null); }
How can I write a file created by FileSavePicker?
source share