Asked by:
Creating and Writing to a new file upon selecting destination in FileSavePicker

Question
-
I'm having a little difficulty figuring out what the cause of this error is. I've added FilePicker capabilities in the Manifest, and it's not like I'm trying to do anything crazy; just trying to save to a sub folder within the Documents folder...
Error:
"An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dllAdditional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
I have confirmed that my User Account is Admin and that it has Full Control over folders and files. But I'm not sure what else I can try. I'm only new to writing data to the drive in Metro apps and I have to admit, I am struggling :/
Any help would be appreciated.
Edit:The code I am using is below:
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); }
- Edited by J. Kent Tuesday, December 6, 2011 1:50 AM Added Code (C#)
Monday, December 5, 2011 10:26 PM
All replies
-
Hi J, did you add the Documents Library access in your manifest? -Jeff
Jeff Sanders (MSFT)Tuesday, December 6, 2011 5:24 PMModerator -
Yep, I did Jeff! :-D But still not worky :( At first, I thought it may be a problem with how I wrote the code, but since I was following multiple tutorials on the subjects, I think that's unlikely.
I'm not really sure what to do next.
It's very strange, though. Because I just ran the FileAccess sample for C# and it saved the file to where ever I tried to save it. Then I ran the FilePicker sample, and that worked too.
I also created two of my own sample apps. A FileSavePicker one, and a WriteFile one. Both of which work on their own, but when combined, the file gets created by the FileSavePicker with zero-length, but the contents still never gets written to that file.
I'm beginning to think that maybe the order in which I am writing to the file that has been created may be wrong?
Jase
Tuesday, December 6, 2011 8:52 PM -
Jase,
You can send me an email from here with your project zipped up: http://blogs.msdn.com/b/jpsanders/contact.aspx
If I can recreate the issue I can debug it easier and save us both some time I hope!
-Jeff
Jeff Sanders (MSFT)Tuesday, December 6, 2011 9:01 PMModerator -
Jeff,
I am unable to upload files.
I'm not sure what is going on, but I cannot upload attachments to Hotmail, or any other e-mail service. I also tried uploading the zip file to Windows Live SkyDrive service to a public folder but after selecting a file to upload (using any service), nothing happens. It's as if I never clicked the 'upload' or 'attach' button to begin with. This is very strange.
Could it be that Windows Dev Preview is not letting apps/sites access the filesystem?
[*Edit*]
Finally able to attach files. Email sent!
Thanks
jase
- Edited by J. Kent Sunday, December 11, 2011 3:17 AM
Saturday, December 10, 2011 4:23 AM -
Strange issue Jase... I have not had any problems like that using Win8 (and I have been using it for months). For this 2nd issue, please open a thread in the General OS Forum and let's see if someone over there could have a hint for what this problem is.
Jeff Sanders (MSFT)Monday, December 12, 2011 1:17 PMModerator