Reader Level:
Articles

Retrieve All Items From Resource Files

By Ibrahim Ersoy on October 23, 2012
While developing for the Windows 8 Platform, you'd like to use items in a resource file for a Windows store application. Here I'll show you how to get all the strings in a Resource file.
  • 0
  • 0
  • 5255

If you're planning to use Resource Files to write items and then read them, you're on the wrong track.


Because Windows Store Applications don't support ResourceWriter classes, but that doesn't mean you can't use Resource Files!

Before beginning working with Resource Files, make sure you've created a Windows Store Application (C# and XAML) and added a Resource File to your project.

Name it "Resource.resw" or anything not related to a folder name in your project.

Add two items such as:

Name    |          Value
-----------------------------------
Note1         First Note
Note2         Second Note
 

Then in your application add these namespaces:

using Windows.ApplicationModel.Resources.Core;
using Windows.ApplicationModel.Resources;

Now create 2 variables:

List<string> reslist = new List<string>();
ResourceMap rmap = ResourceManager.Current.MainResourceMap;

Using the ResourceManager's MainResourceMap we're retrieving all the maps provided and storing these maps in a ResourceMap variable.

Now add this code to retrieve all the string items from the Resources file.We used substring while querying because the original query gives folder names too, so we had to crop the useful part from it. Resources is 9 digits + 1 (for removing backslash character too).

foreach (string str in rmap.Keys) 
{ 
if ((str.StartsWith("Resources"))) 
{reslist.Add(str.Substring(10));} 

else 
{
}

}
foreach (string str in reslist) 
{
ResourceLoader rl = new ResourceLoader("Resources");listBox1.Items.Add(rl.GetString(str));
}

Finally we're listing all the strings in a listbox:


ss1.png



Hope it helps you to.


Ibrahim Ersoy

Ibrahim Ersoy lives in Turkey, Istanbul. He helps running and managing C# Corner. He is a Software Consultant (Sharepoint) and MindCracker MVP. He writes about cutting-edge Microsoft Technologies including XNA,... Read more

Personal Blog: http://www.iersoy.com
COMMENT USING

Trending up