.NET Framework CLR Tools: How do I use the Resource File Generator?
Q: How do I use the Resource File Generator?
A: The Resource File Generator converts '.txt' files and '.resx' (XML-based resource format) files to common language runtime binary '.resources' files that can be embedded in a runtime binary executable or compiled into satellite assemblies.
What conversions does the 'rResGen.exe' tool perform?
'Resgen.exe' performs the following conversions:
Converts '.txt' files to '.resources' or '.resx' files.
Converts '.resources' files to text or '.resx' files.
Converts '.resx' files to '.txt' or '.resources' files
What classes does the resource generator use?
'ResourceReader' class
'ResourceWriter' class
'ResXResourceReader' class
'ResXResourceWriter' class
What does the 'ResourceReader' class do?
The 'ResourceReader' provides a default implementation of the 'IResourceReader' interface.
You can use resource readers to read resource name and resource value pairs from '.resources' files.
The resources can be enumerated by traversing the 'IDictionaryEnumerator' returned by the 'GetEnumerator()' method.
The methods provided by the 'IDictionaryEnumerator' are used to advance to the next resource and read the name and value of each resource in the '.resources' file.
Can I have an example using the 'ResourceReader' class?
The following code sample displays the content of 'myResources.resources' file on the console:
Code:
Imports System
Imports System.Resources
Imports System.Collections
Public Class ReadResources
Public Shared Sub Main()
' Opens a resource reader and get an enumerator from it.
Dim reader As New ResourceReader("myResources.resources")
Dim en As IDictionaryEnumerator = reader.GetEnumerator()
' Goes through the enumerator, printing out the key and value pairs.
While en.MoveNext()
Console.WriteLine()
Console.WriteLine("Name: {0}", en.Key)
Console.WriteLine("Value: {0}", en.Value)
End While
reader.Close()
End Sub
End Class
What does the 'ResourceWriter' class do?
Writes resources in the system-default format to an output file or an output stream.
Can I have an example using the 'ResourceWriter' class?
The following example writes several strings into the 'myResources.resources' file:
Code:
Imports System
Imports System.Resources
Public Class WriteResources
Public Shared Sub Main()
' Creates a resource writer.
Dim writer As New ResourceWriter("myResources.resources")
' Adds resources to the resource writer.
writer.AddResource("String 1", "First String")
writer.AddResource("String 2", "Second String")
writer.AddResource("String 3", "Third String")
[COLOR=#880000]' Writes the resources to the file or stream, and closes it.
writer.Close()
End Sub
End Class
What does the 'ResXResourceReader' do?
You can use resource readers to read resource name and value pairs from '.resx' files.
The resources can be enumerated by traversing the 'IDictionaryEnumerator' returned by the 'GetEnumerator()' method.
You can use the methods provided by the 'IDictionaryEnumerator' to advance to the next resource and read the name and value of each resource in the '.resx' file.
Can I have an example using the 'ResXResourceReader' class?
The following example demonstrates how to use a 'ResXResourceReader' to iterate through the resources in a '.resx' file. First, the
Code:
ResXResourceReader rsxr
is created for the file 'items.resx'. Next, the 'GetEnumerator()' method is used to create an 'IDictionaryEnumerator' to iterate through the resources and display the contents to the console:
Code:
Imports System
Imports System.Resources
Imports System.Collections
Imports Microsoft.VisualBasic
Class ReadResXResources
Public Shared Sub Main()
' Create a ResXResourceReader for the file items.resx.
Dim rsxr As ResXResourceReader
rsxr = New ResXResourceReader("items.resx")
' Create an IDictionaryEnumerator to iterate through the resources.
Dim id As IDictionaryEnumerator = rsxr.GetEnumerator()
' Iterate through the resources and display the contents to the console.
Dim d As DictionaryEntry
For Each d In rsxr
Console.WriteLine(d.Key.ToString() + ":" + ControlChars.Tab + d.Value.ToString())
Next d
'Close the reader.
rsxr.Close()
End Sub
End Class
What does the 'ResXResourceWriter' class do?
Writes resources in an XML resource ('.resx') file or an output stream.
How do I use the 'ResGen' tool?
You run the 'Resgen.exe' tool from the command prompt:
Bookmarks