Read content files #Metro apps #Wind8Dev #WinRT

I have started working on @Wordastic for #Windows8. Windows 8 is a weird beast.. you code in C# but you don’t code for CLR you code for #WinRT.

WinRT or the Windows Run Time as the name suggests is a OS layer that exposes functionality to all languages C / C++/ .NET and JS. This means that things change in many ways. For starters all methods are async. You need to use await to implicitly add callbacks etc. no need for Thread.Sleep etc.

Lets say that in our project we have “Content” directory that contains “MyTest1.txt” file which is included as Content. How do we read it.. well here it is

string myFile = Path.Combine(Package.Current.InstalledLocation.Path, "Content/MyTest1.txt");

StorageFolder myFolder = await StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(myFile));
                
using (Stream s = await myFolder.OpenStreamForReadAsync(Path.GetFileName(myFile)))
{
    using (StreamReader sr = new StreamReader(s))
    {
        // do whateve you want
    }
}

Package.Current.InstalledLocation gives you the path to the app installations. then you navigate forward to the content path and you open the stream. At each step you use await to cause execution to return at desired point to continue execution e.g. after getting access to storage folder or opening the file for reading.

That’s it for today. more later.

Advertisements

One thought on “Read content files #Metro apps #Wind8Dev #WinRT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s