Writing to a file using FileSavePicker

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?

+4
source share
3 answers

You do not need to call StorageFolder.GetFolderFromPathAsync(savedItem.Path) and sFolder.GetFileAsync(savedItem.FileName) . You must delete these two lines because they throw an exception. You must use the StorageFile object that was returned by savePicker.PickSaveFileAsync() , because this object has all permissions. Then you can just call savedItem.OpenAsync(FileAccessMode.ReadWrite) .

+4
source

You probably do not have the “Access Document Library” in terms of the appxmanifest features of your application. Without this feature, windows will restrict access to the file system. Similar possibilities exist for music, video and image libraries.

You have already added a “File Picker” to the ads section, which is probably not the one you want. The “File Picker” ad indicates that if any other application calls the file picker, your application will be listed as a possible source of files.

+2
source

I also found that adding video or image access capabilities to Manifest only takes effect after restarting Windows 10. Perhaps this is a problem with my computer, but I think it's worth sharing.

0
source

Source: https://habr.com/ru/post/1384964/


All Articles