locked
C# Metro - StorageFolder Problem - can't write to file RRS feed

  • Question

  • I'm making a StorageFolder/File with the name "sample.txt".

    Then I'm saving .xml code from "http://www.djmebbe.com/feed/" to a variable.

    I then want to write whats saved in the .xml variable "xmlHolder", to my sampleFile "sample.txt"

    Code:

    But I get error at the first line when doubleTapping my TextBlock...

    Pls help me.


    StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
                    StorageFile sampleFile = await storageFolder.CreateFileAsync("sample.txt");
    
                    var xmlHolder = await new System.Net.Http.HttpClient().GetStringAsync("http://www.djmebbe.com/feed/");
    
                    await Windows.Storage.FileIO.WriteTextAsync(sampleFile, xmlHolder);
    
                    
               var  txtHolder = sampleFile;

    • Edited by ImNew54 Tuesday, January 15, 2013 2:19 PM
    Tuesday, January 15, 2013 2:17 PM

Answers

  • you cannot save to the documents folder with out user interaction.  use a  savepicker to save to that folder.  You can save to the local folder with out user interaction
    • Marked as answer by ImNew54 Tuesday, January 15, 2013 9:27 PM
    Tuesday, January 15, 2013 2:36 PM
  • Actually that is not entirely true. You can definitely access DocumentsLibrary programatically.

    BUT... there is a big "but"... you would need to add a File Type Association declaration for TXT files and the Documents Library capability. And documents library capability is considered a special capability so if you want to certify your application, you really need to have a good reason to have this capability which is not quite desirable if in most cases.

    You can however, use the file picker only once as Ken was suggesting, and add the file to the future access list. So your program will have access to this specific file.

    Or simply you can use the local storage for your application without having to worry about the capabilities, file pickers etc.


    Can Bilgin
    Blog CompuSight

    • Proposed as answer by Dave SmitsMVP Tuesday, January 15, 2013 4:22 PM
    • Marked as answer by ImNew54 Tuesday, January 15, 2013 9:27 PM
    Tuesday, January 15, 2013 4:20 PM

All replies

  • you cannot save to the documents folder with out user interaction.  use a  savepicker to save to that folder.  You can save to the local folder with out user interaction
    • Marked as answer by ImNew54 Tuesday, January 15, 2013 9:27 PM
    Tuesday, January 15, 2013 2:36 PM
  • Actually that is not entirely true. You can definitely access DocumentsLibrary programatically.

    BUT... there is a big "but"... you would need to add a File Type Association declaration for TXT files and the Documents Library capability. And documents library capability is considered a special capability so if you want to certify your application, you really need to have a good reason to have this capability which is not quite desirable if in most cases.

    You can however, use the file picker only once as Ken was suggesting, and add the file to the future access list. So your program will have access to this specific file.

    Or simply you can use the local storage for your application without having to worry about the capabilities, file pickers etc.


    Can Bilgin
    Blog CompuSight

    • Proposed as answer by Dave SmitsMVP Tuesday, January 15, 2013 4:22 PM
    • Marked as answer by ImNew54 Tuesday, January 15, 2013 9:27 PM
    Tuesday, January 15, 2013 4:20 PM
  • Okey =)

    Thanks guys!

    Tuesday, January 15, 2013 9:29 PM