Retrieve All Items From Resource Files

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.


Next Recommended Readings
ARAF GLOBAL
Araf Global is a software consultancy company founded in 2016 focusing on cutting edge technologies.