locked
Is there a known bug in Dev Preview or VS Expr'ss Prev. that prevents files from being written to disk in new apps? RRS feed

  • Question

  • I have the following code (pasted directly from the FileAccess sample. All the options are checked (Document Directory access in Capabilities etc) but it still doesn't work.

     

     

            async void Button_Click(object sender, RoutedEventArgs e)
            {
                // irrelevant code removed.
    
                // Now we can add the new note.
                note.Margin = ThicknessHelper.FromLengths(5, 0, 50, 0);
    
                Holder.Children.Add(note);
    
                StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
                StorageFile sampleFile = await storageFolder.CreateFileAsync("jasonrules.txt");
    
    
                try
                {
                    IRandomAccessStream writeStream = await sampleFile.OpenAsync(FileAccessMode.ReadWrite);
    
                    //textbox input not yet implemented due to bug353726
                    //string writeToFile = textbox1.Text;
    
                    IOutputStream outputStream = writeStream.GetOutputStreamAt(0);
                    DataWriter dataWriter = new DataWriter(outputStream);
                    dataWriter.WriteString(note.GetText());
    
                    await dataWriter.StoreAsync();
                    outputStream.FlushAsync().Start();
    
                }
                catch (Exception)
                {
                    MessageDialog msg = new MessageDialog("Something went wrong.", "Error");
                    msg.ShowAsync();
                }
            }
    
    
    <br/>
    
    Am I missing something here? Has anyone had any success writing files to disk?
    


     

    The FileAccess Sample(s) work perfectly. And even though I've pretty much made a complete clone of the Write and CreateFile methods, and enabled/chosen appropriate access settings (such as in Capabilities tab), my one doesn't work.
    Sunday, December 11, 2011 10:42 PM

Answers

  • Your code works for me.  Are you getting an exception or is everything running but nothing happens?  If you are getting an exception, what is it?

    My guess is that you're getting Access denied at the CreateFileAsync call.  Even with the Document Library Access capability the app can only access file types that it can handle.  Is your test app declared to handle .txt files?

    --Rob

     

    • Proposed as answer by Anton K2 Wednesday, December 14, 2011 6:16 AM
    • Marked as answer by Jie Bao Tuesday, January 3, 2012 7:45 AM
    Tuesday, December 13, 2011 8:11 PM
    Moderator

All replies

  • I have the similar problem. I cannot get files from the pictures library. GetFileAsync throws System.UnauthorizedAccessException even though I added the picture access capability.
    • Edited by Anton K2 Tuesday, December 13, 2011 11:55 AM
    Tuesday, December 13, 2011 11:55 AM
  • Your code works for me.  Are you getting an exception or is everything running but nothing happens?  If you are getting an exception, what is it?

    My guess is that you're getting Access denied at the CreateFileAsync call.  Even with the Document Library Access capability the app can only access file types that it can handle.  Is your test app declared to handle .txt files?

    --Rob

     

    • Proposed as answer by Anton K2 Wednesday, December 14, 2011 6:16 AM
    • Marked as answer by Jie Bao Tuesday, January 3, 2012 7:45 AM
    Tuesday, December 13, 2011 8:11 PM
    Moderator
  • Wednesday, December 14, 2011 5:49 AM
  • Thanks, Rob.

    I added an .txt extension and the code started working.

    Wednesday, December 14, 2011 6:17 AM