Derik Whittaker



Creating an IStorageFile out of a Local Resource in WinRT (C#)

When working with a C#/XAML WinRT application it is common that you will include resources such as images, files, audio recordings, etc inside your deployment package.  It will also be common that you may want to access these resources as native storage files in order to do things like share them between applications.  Accessing these files is actually pretty easy and is only about 3 lines of code.

Before we get started assume that we have a directory structure inside our application as follows


When accessing our local files we will first want get the StorageFolder of the InstalledLocation of our package.  This can be done as follows

var packageLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;

Now that we have the StorageFolder (packageLocation) we want we can get the “Images” folder as follows

var imagesFolder = await packageLocation.GetFolderAsync("Images");

Once we have our images folder it is simple as asking for the given file as follows

var image = await imagesFolder.GetFileAsync(“Image1.png”);

Now that we have our Image we also have our IStorageFile.  This allows us to pass this file to another application via the Share Contract, or to create a stream reference out of via the RandomAccessStreamReference.CreateFromFile(image) method.

Here is all the code in one block

var packageLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
var imagesFolder = await packageLocation.GetFolderAsync("Images");
var image = await imagesFolder.GetFileAsync(“Image1.png”);

// if you want the file as a stream (say to put into a Bitmap control
var imageStreamRef = RandomAccessStreamReference.CreateFromFile(image);

As you can see from above, this is pretty easy as well as pretty powerful.

Till next time

Posted 06-26-2012 8:52 AM by Derik Whittaker
Filed under: , ,



Jeremy Likness wrote re: Creating an IStorageFile out of a Local Resource in WinRT (C#)
on 06-27-2012 2:16 PM

It's actually easier than that, Derik. You can use the PathIO helper with the special path modifier to access the package location in local storage. This can be done in a single line of code:

var imageBuffer = await PathIO.ReadBufferAsync("ms-appdata:///Images/Image1.png");            

If you had the image embedded as content (not in local storage) you would swap ms-appdata with ms-appx.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)