Reading a .RES Resource File
This tutorial will show how to iterate over the resources that
can be found in a .RES resource file.
A .RES resource file can contain many types of resources. Each resource is associated to a
specific language. In VBCorLib, languages are identified by the LCID of a CultureInfo object.
Using the ResourceReader a specific .RES file can be opened and iterated over to retrieve
all resources contained in it. The ResourceReader only recognizes a subset of the the resource
types that can be found in a .RES file. These types are String, Bitmap, Icon and Cursor. All other
types, including custom types, are read as Byte arrays.
This tutorial assumes a .RES file named Resources.RES exists in the App.Path folder. In the example, the
Resources.RES file contains two String tables [English (United States), Spanish (Mexico)],
each containing two words [Hello, Bye, Hola, Adiós]. All resource .RES files are
iterated the same way using the ResourceReader class.
To create a ResourceReader object, the filename of the specific .RES file is passed to
the ResourceReader constructor.
Dim r As ResourceReader
Set r = Cor.NewResourceReader(Path.Combine(App.Path, "Resources.res"))
As shown, creating a ResourceReader is performed the same way as creating many objects
in VBCorLib. The App.Path is combined with the Resources.res filename using the Path
object. The output of Path.Combine will be a properly formatted full file path.
Once a ResourceReader has been created, the resources are read in and stored, ready
for iteration. To iterate using the ResourceReader we will use the For..Each convention.
The ResourceReader returns an IDictionaryEnumerator when it is to be enumerated.
This type of enumerator allows for both a Key and a Value to be accessed during the enumeration.
The Key is the Resource ID or Ordinal and may not be unique across all resources in the .RES file.
For this reason, the Key should not be used as a unique resource identifier.
The value of an IDictionaryEnumerator is a DictionaryEntry object providing both
a Key and a Value for each enumeration. So when enumerating the resources, we expect a
DictionaryEntry object to be returned. The Value property of the DictionaryEntry
does not return the resource itself. It returns a Win32Resource object which contains
many details about the resource, including the resource value itself.
Dim de As DictionaryEntry
Dim res As Win32Resource
For Each de In r
Set res = de.Value
Console.WriteLine "Language: {0}, ID: {1}, Value: {2}", res.LanguageID, res.Ordinal, res.Value
Next de
'' This code produces the following output
''
'' Language: 1033, ID: 101, Hello
'' Language: 1033, ID: 102, Bye
'' Language: 2058, ID: 101, Hola
'' Language: 2058, ID: 102, Adiós
As shown in this portion of the code, we declare two variables to help iterate through the resources.
The de is the returned value of the For..Each enumeration. From the de object we can
obtain the Win32Resource object using the Value property of de.
This simply displays some details of the Win32Resource object in the Console.
Iterating though the resources of a .RES file is straight forward. The DictionaryEntry
object does not contain the resource itself, but a Win32Resource object, which contains
several details about the resource and the resource itself.
By enumerating through the resource, specific resources can easily be found for later use.
In this sample, we created a ResourceReader that is designed to iterate through all the
resources in a .RES file. Resources for any cultures are included in the iteration. The resource
ID is not unique across all resources in the .RES file and should not be used for unique identification.
To help with culture specific resource handling, the ResourceSet or ResourceManager should be used.
I hope this sample has shown how easy it is to iterate and retrieve all resources within a .RES file.
|