Access your Application Assets folder

imageNormally when you start developing application in XAML and windows 8 you will include some files in your solution that you will use to display pictures, data and read xml files.

I’m a Developers and like all developers I love to jump in code then start thinking how can I do something when I face a problem, but you need to know a few things before this understanding the namespace Windows.Storage, Default paths and files



Windows.Storage Namespace

If you take a look at the Windows.Storage documentation, you'll discover that the Windows.Storage namespace has a StorageFolder class that lets you manipulate folders and their content. By exploring the class further, you'll find that it contains a method called StorageFolder.CreateFileAsync. This method will asynchronously create a new file in the current folder.

You will find also a method for getting files, read files attributes .. etc

In my Below Example I’ve added an XML document that contains list of Countries to retrieve and display in my Application.

first you need to know where is your assets folder is installed on the user machine.

  1. StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

then you will need to read the files from the location you added it to the assets folder

  1. string CountriesFile = @"Assets\Countries.xml";
  2.             StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
  3.             StorageFile file = await InstallationFolder.GetFileAsync(CountriesFile);

In my case I converted the file into steam and started using Linq for XML and started reading the data from the XML

Below If my Full Code listing for the Load countries Method it might be useful.

  1. public async void LoadCountriesXML()
  2.         {
  3.             string CountriesFile = @"Assets\Countries.xml";
  4.             StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
  5.             StorageFile file = await InstallationFolder.GetFileAsync(CountriesFile);
  6.             Stream Countries = await file.OpenStreamForReadAsync();
  8.             XDocument xDOC = XDocument.Load(Countries);
  9.             IEnumerable<XElement> listofCountries = from country in xDOC.Descendants("Country")
  10.                                                     select country;
  12.             foreach(XElement item in listofCountries)
  13.             {
  14.                 TripCountryView tripCountryView = new TripCountryView();
  15.                 tripCountryView.SetControlData(item.Attribute("Name").Value, item.Attribute("ID").Value, item.Attribute("FlagImage").Value);
  17.                 pnlCountries.Items.Add(tripCountryView);
  18.             }
  19.         }