locked
Wanting to read a file from my Metro Project / How?

    Question

  • I have a Metro project that I've put a file in the root directory + "data" called:

    C:\VSProjects\USGov\data\myfile.json

    I want to read this file and can't figure out how to.  I can read files in a temp directory using code like I have below, but I want to include this file in my distribution.  

    Once I figure this out, I'm going to be hit with a second problem which is readString fails with an error on my JSON. How can I read this large json file (.5MB) without getting errors.

     

    dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {
                    var size = stream.size;
                    if (size == 0) {
                        // 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);
    ...
    


     


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Monday, January 02, 2012 9:36 PM

Answers

  • Hi Jeff,

    "StorageFile                 sf = await Package.Current.InstalledLocation.GetFileAsync" means nothing to me.  Is Package a namespace? it does not show up in the WinRT api list for JavaScript (or c# for that matter which is where it is posted).

    Can you please give me an example in JavaScript similar to my code above that will allow me to open and read the file.  I combined it in one questions because open and read are usually together in the same example.  If you still want me to create another thread for this question I will but to me it seems unnecesary.



    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Tuesday, January 03, 2012 4:39 PM
  • Sorry Peter I go back and forth between the two I forgot where you were posting and put in the C# thread.

    The documentation has an example of how to use these objects for each language (normally).

    Here is the documentation for that API:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.package.installedlocation.aspx

    You should be able to use that the same way you are using storage folders in javascript today.

    If you need some examples for using storage folders, check out the documentation or samples and they should help you along (any of the picker examples should be adequate!

    -Jeff


    Jeff Sanders (MSFT)
    Tuesday, January 03, 2012 4:49 PM
    Moderator

All replies

  • Hi Peter,

    You can get the install location and read the files from there:

    http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/8aa44156-35e2-40c1-9aba-a67856af7e74

    Please create another thread for your other question.

    -Jeff


    Jeff Sanders (MSFT)
    Tuesday, January 03, 2012 4:04 PM
    Moderator
  • Hi Jeff,

    "StorageFile                 sf = await Package.Current.InstalledLocation.GetFileAsync" means nothing to me.  Is Package a namespace? it does not show up in the WinRT api list for JavaScript (or c# for that matter which is where it is posted).

    Can you please give me an example in JavaScript similar to my code above that will allow me to open and read the file.  I combined it in one questions because open and read are usually together in the same example.  If you still want me to create another thread for this question I will but to me it seems unnecesary.



    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Tuesday, January 03, 2012 4:39 PM
  • Sorry Peter I go back and forth between the two I forgot where you were posting and put in the C# thread.

    The documentation has an example of how to use these objects for each language (normally).

    Here is the documentation for that API:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.package.installedlocation.aspx

    You should be able to use that the same way you are using storage folders in javascript today.

    If you need some examples for using storage folders, check out the documentation or samples and they should help you along (any of the picker examples should be adequate!

    -Jeff


    Jeff Sanders (MSFT)
    Tuesday, January 03, 2012 4:49 PM
    Moderator
  • I've recreated the post with the multi-byte error here:

     

    http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/6c989bba-0455-41db-b414-7a30fd6904b5

     


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Tuesday, January 03, 2012 7:29 PM
  • Thanks Jeff! I just did a post explaining everything you explained with working source.  Hopefully, i if Microsoft does not change all this when beta and production come a long, it might help someone else.

    http://peterkellner.net/2012/01/03/with-windows-8-metro-html5css3javascriptwinrt-read-any-file-from-project-directory/



    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Tuesday, January 03, 2012 9:28 PM
  • That is awesome Peter, thanks for helping the Community with your Post!

    -Jeff


    Jeff Sanders (MSFT)
    Tuesday, January 03, 2012 9:30 PM
    Moderator
  • You are most welcome.  More to come!  I'm trying to build a pretty straight forward app for the store contest which I need to finish this week (deadline 1/8).  I'm planning on making it public and doing lots of posts about it (win or not). I need to get back to my SVG stuff now (not Canvas) so I can make the state outline.

    Here is what it currently looks like on the first page:


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Tuesday, January 03, 2012 9:38 PM
  • I cannot seem to get this to work with the full release??

    I keep getting access denied?

    Monday, November 12, 2012 5:09 PM