I want to read a text file in Javascript/Metro RRS feed

  • Question

  • Could we start at a more simplistic level?

    I want to read a text file in Javascript/Metro.

    Where should the file go in my file-system? I know about applicationData.localfolder but is this definitely supported by Metro? The file access sample they give puts the sample.txt when created into the documents folder and you're required to give it capabilities for that folder.

    How can I be so confused over something as simple as reading a text file?


    Monday, February 20, 2012 2:32 AM


  • Hi Jerry,

    Yes, Peter's example should work fine for you.  You should not use a \ or / in your particular code.

    In his sample the json file would be in \data\NAME OF JSON FILE HERE.


    Jeff Sanders (MSFT)

    Tuesday, February 21, 2012 1:49 PM

All replies

  • Hi Jerry,

    application.localFolder is indeed supported.  That is why we published that in the documentation.  Is there a reason you think it is not supported?

    There are a couple of options for text files.  You need to determine what you are using this file for and then choose the appropriate method for your particular application.

    These questions will help you scope your questions easier:

    What are you doing with this file?  Are you reading and writing to it and why?

    If it is simply to save some data IOHelper is a great way! http://msdn.microsoft.com/en-us/library/windows/apps/br211764.aspx

    It is instantiated from the local, roaming or temp fields of the application object like this:




    Jeff Sanders (MSFT)

    Monday, February 20, 2012 7:50 PM
  • Hi Jeff,

    Sorry for putting my question in the wrong place, I didn't realise it would be seen as hijacking.

    I think the document capabilities thing really threw me, as you may of guessed I'm not that savvy with modern coding... it's been a good ten years since I wrote a line.

    I'm going to go away and try some new things and report back if I have problems, thanks very much for your reply.


    Monday, February 20, 2012 11:20 PM
  • Hi Jeff,

    Here's 'my' code (it's actually mainly yours from the original thread, edited about by me).

    'test.txt' is a text file that lives in the same directory as default.html

    When run the size==0 condition takes effect and my code sends back 'data not found'.

    Any help would be very much welcome.


    This is default.html

    <!DOCTYPE html>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=1024, height=768" />
        <!-- WinJS references -->
        <link rel="stylesheet" href="/winjs/css/ui-dark.css" />
        <script src="/winjs/js/base.js"></script>
        <script src="/winjs/js/wwaapp.js"></script>
        <script src="/winjs/js/ui.js"></script>
        <script src="/winjs/js/controls.js"></script>
        <!-- fileplay references -->
        <link rel="stylesheet" href="/css/default.css" />
        <script src="/js/default.js"></script>
        <script src="/js/myscript.js"></script>
        <div data-win-control="WinJS.UI.ViewBox">
            <input type="button" onclick="loadtxt();" value="Click Me" />
            <div id="mytext" class="fixed-layout">
                <p>Content goes here</p>

    This is myscript.js

        function id(id) {
            return document.getElementById(id);
    function loadtxt() {
        var applicationData = Windows.Storage.ApplicationData.current;
        var localFolder = applicationData.localFolder;
            localFolder.createFileAsync("test.txt", Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {
                dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {
                    var size = stream.size;
                    if (size == 0) {
                        // Data not found
                        id("mytext").innerHTML = "data not found";
                    else {
                        var inputStream = stream.getInputStreamAt(0);
                        var reader = new Windows.Storage.Streams.DataReader(inputStream);
                        reader.loadAsync(size).then(function () {
                            var contents = reader.readString(size);
                            id("mytext").innerHTML = contents;

    Monday, February 20, 2012 11:52 PM
  • Hi Jerry,

    I think your problem is that the text file does not exist in the location you think it does.  If that is true however, I would not expect the code to get all the way to the 'data not found' part of your code.

    The local folder is not the same folder that your application stores the application ('test.txt' is a text file that lives in the same directory as default.html"). 

    The local, roaming and temp folders are set aside for your reading and writing data to.  The application directories created by the app install are different.  You cannot write data in the install directory.

    Is test.txt some static html you wish to include in your application?  That would make it a great candidate for an HTML fragment instead.

    If test.txt is simply information you wish to read then you can get to the application installation directories and read this.

    Can you give me some more background on what you are going to do with this file in your finished app?  That would allow me to suggest something that is appropriate for your case.


    Jeff Sanders (MSFT)

    Tuesday, February 21, 2012 12:57 PM
  • Hi Jeff,

    The text file will be packaged with my project to act as a settings file. It doesnt need to be written to. At this point it's also just a learning project to get back into program development at any level.

    I've looked into this more and came across Peter Kellner's page which looks to do what I want, but using the following lines proved unsuccessful:

       4:      var package = Windows.ApplicationModel.Package.current;
       5:      var installedLocation = package.installedLocation;

    I'm going to install Peter's whole solution when I have time and see if that runs properly.

    One thing I noticed in my code a few messages above is that if I change "test.txt" to "\test.txt" or "/test.txt" the code neither gets to data not found nor the section where it is found. It effectively dies on the createfileAsync call, not even raising an exception in the debugger.

    Thanks very much for your help.


    Tuesday, February 21, 2012 1:35 PM
  • Hi Jerry,

    Yes, Peter's example should work fine for you.  You should not use a \ or / in your particular code.

    In his sample the json file would be in \data\NAME OF JSON FILE HERE.


    Jeff Sanders (MSFT)

    Tuesday, February 21, 2012 1:49 PM
  • Hi Jeff,

    Had some success with Peter Kellner's code and can now read my text file... when I can deploy it, because I've started getting a lot of errors like this:

    Error    1    Error DEP0700 : Registration of the application from layout "C:\Users\gray\AppxLayouts\73bc0058-d95f-411a-93ce-e1a7e8965355VS.Debug.AnyCPU.gray" failed for the following reason (please check the event viewer for possible additional details):    loadfile

    Error    2    Error while deleting existing state. Error Code: 0x80070005.     loadfile

    When hitting f5 to deploy and run the code.

    I've looked at the other thread about this issue but the identity thing seems to be okay and I can't see what's wrong.  I did twice see another error about it not being able to delete thumbs.db because another program was using it but was  unable to replicate this.

    - jerryhat

    • Edited by jerryhat Friday, February 24, 2012 1:05 AM
    Friday, February 24, 2012 1:01 AM