Derik Whittaker

Syndication

News


Reading a embedded content file in WinRT & C#

Have you ever had the need to read in an embedded content file when working in WinRT, how about using the PCL with WinRT?  Maybe you have a .json or .xml file you want to use to test data (which is my case).

There are a ton of different suggestions on how to accomplish this on the net, I found most of them to partially accurate or just down right wrong.  Because of this I thought I would demonstrate how I was able to accomplish this.

Step 1: Adding the file to be read to your project:

Below is my file to be read and its folder structure.  You can see that my file is 2 folders off of the root.

image

Step 2: Setting the properties for this file:

Below is the screen shot of the properties panel (Right click the file and select properties). Pay attention to the fact that my Build Action is ‘Content’

image

Step 3: Create the logic to read in the file and return the contents as a string

Below is the code I used to read in the file.

public async static Task ReadJson()
{
    var jsonFile = Path.Combine(Package.Current.InstalledLocation.Path, "JsonSerialization/json/DirectorySearchResults.json.txt");

    var jsonFolder = await StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(jsonFile));

    using (var jsonStream = await jsonFolder.OpenStreamForReadAsync(Path.GetFileName(jsonFile)))
    {
        using (var streamReader = new StreamReader(jsonStream))
        {
            var json = streamReader.ReadToEnd();
            return json;
        }
    }
}

When looking at the code above there I do need to call attention to the first line.

In the first line I am creating the path to my json file.  When providing the name of the file make sure to provide the name of EACH folder in the file path.  If you do not do this you will not like your results.

Hope this helps.

Till next time,


Posted 01-17-2014 9:07 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Nigel Sampson wrote re: Reading a embedded content file in WinRT & C#
on 01-17-2014 7:28 PM

Thanks, there's also a couple of helper methods you can use to make the code a bit simpler.

var jsonFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///jsonserialisation/json/directorysearchresults.json.txt"));

var json = FileIO.ReadTextAsync(jsonFile);

ms-appx lets you create uri's to files in your installed location and FileIO contains a lot of one liner helper methods.

Jason Haley wrote Interesting Finds: January 18, 2014
on 01-18-2014 7:33 AM

Interesting Finds: January 18, 2014

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)