locked
Load embedded file RRS feed

  • Question

  • I try to load a file delivered with my project into memory.

    How would I do that?

    Embedd the file as Resource (marked in Properties) or just "Copy always"?

    But where will I find the file then?

    WinRT is strangely new :)

    Saturday, September 24, 2011 6:12 PM

Answers

  • Mark it as content and copy if newer.

    Then it will be delivered with your package.  To load the file (using reading an HTML file contents as an example):

    var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"folder\ index.html");
    var stream = await file.OpenForReadAsync();
    var rdr = new StreamReader(stream.AsStream());
    
    Task.Run(() =>
    {
    var contents = rdr.ReadToEnd();
    });
    
    

    The Task.Run is required as ReadToEnd is locking and ReadToEndAsync is not available in the preview bits.


    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer
    • Marked as answer by Tamlocar Sunday, September 25, 2011 8:10 AM
    Saturday, September 24, 2011 11:55 PM

All replies

  • Mark it as content and copy if newer.

    Then it will be delivered with your package.  To load the file (using reading an HTML file contents as an example):

    var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"folder\ index.html");
    var stream = await file.OpenForReadAsync();
    var rdr = new StreamReader(stream.AsStream());
    
    Task.Run(() =>
    {
    var contents = rdr.ReadToEnd();
    });
    
    

    The Task.Run is required as ReadToEnd is locking and ReadToEndAsync is not available in the preview bits.


    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer
    • Marked as answer by Tamlocar Sunday, September 25, 2011 8:10 AM
    Saturday, September 24, 2011 11:55 PM
  • Thx. That's working :)
    Sunday, September 25, 2011 8:10 AM
  • Hi Tim, 

    I'm working on a app where I put all my model logic in an other project (let's call this project "MyClassLibrary").

    In this project, I've got a JSON file (located in MyClassLibrary/Resources/MyJsonFile.json) and a "FooService" class who wants to read this JSON file (and get a string or a Stream). 

    I tried some code from your other forum posts you've made, with the ResourceLoader and the ResourceMap. But there was no success (maybe I'm doing wrong). 

    If you (or anybody else ;-) ) has a clue... 

    Thank you.

    Michel


    • Proposed as answer by MichelMongkhoy Monday, August 13, 2012 8:42 AM
    • Unproposed as answer by MichelMongkhoy Monday, August 13, 2012 8:42 AM
    Monday, April 16, 2012 4:51 PM
  • Hi Mmongkhoy,

    Is the file a .resjson or .json file? The ResourceLoader and ResourceMap are only able to load from .resjson files. It should be a simple extension rename to .resjson if the file is properly formed JSON.

    Tuesday, April 17, 2012 4:40 PM
  • As I'm working with the Win8 CP, I'd like to update your helpfull post:

    Now the ReadToEndAsync() is indeed available, so we can write:

    var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"folder\ index.html");
    
    var stream = await file.OpenForReadAsync();
    var rdr = new StreamReader(stream.AsStream());
    var contents = await rdr.ReadToEndAsync();



    Saturday, May 26, 2012 9:07 AM
  • And how can I read from file, that marked as content in ClassLiblary dll?

    Thank you!

    Saturday, June 9, 2012 1:30 PM
  • Hi Rylan, 

    Sorry for answering you so late, and thanks for your response. 

    I didn't try your solution yet, but I did find an answer to my problem. 

    It was the path that was wrong. It needed three slashes ( ms-appx:///[MyProjectName]/ ... /myJsonFile.json )

    Here's the rules : 

    • with 2 slashes, you're targeting an extern DLL ; 
    • with 3 slashes, you're targeting the current assembly. 


    • Proposed as answer by MichelMongkhoy Monday, August 13, 2012 8:51 AM
    Monday, August 13, 2012 8:51 AM