[Tutorial] Programmatically Creating / Updating a *.Resx File

Posts 1–4 of 4 · Page 1 of 1
[Tutorial] Programmatically Creating / Updating a *.Resx File
Hi, I was working on a project and needed to add resources to the project in real time. This is done by updating the *.Resx file located in the project directory. Luckily browsing through the Resources namespace I found the ResXResourceWriter class. Before I demonstrate on how to use the class, take a look at what the .Resx file is:

A *.Resx file is a block of XML data that assigns name/value pairs for each resource in your application.

The ResXResourceWriter class provides a set of members that allow you to create the *.Resx files.

Let's create an independent *.Resx file.

■ First create a new project in vb.
■ On the main form, drag a button from the toolbox. Set its text to "Generate *.Resx File".
■ Now double click the button to go to its "Click" event. When the click event shows up, add the following code to it:

'Change to a valid picture location...
Dim PictureToAdd As String = "C:\Users\AUser\Pictures\30651_124206260947142_100 000733059464_175037_5040347_n.jpg"
'Change it to where you want to store your new *.Resx file...
Dim ResxFilename As String = "F:\ResXForm.resx"
'Create a new instance of the Writer...
Dim ResWriter As New ResXResourceWriter(ResxFilename)
'Create the bitmap...
Dim Pic As New Bitmap(PictureToAdd)
'Finally add the picture to your resources, here Kai Hiwatari is the name of the resource and Pic is the value of it. This means that the a new resource will be added with Kai Hiwatari as its name and the picture we defined above as its value...
ResWriter.AddResource( "Kai Hiwatari",Pic)
'Similarly create custom strings...
ResWriter.AddResource( "FlameSaber", "VB.NET Coder")
'Finally create the *.Resx file...
ResWriter.Generate()
'Close the writer so it can free the occupied resources...
ResWriter.Close()

■ Also, add this namespace to the form to refer the ResxResourceWriter class:

Imports System.Resources

Using the similarly technique, you can update the resources of your project in real time. All you need to do is change the variable "ResxFileName" to your project's *.Resx filename.

Here's where your values are added:


This becomes really helpful when you need to give some control to the end user.

Hope this helps !!
Enjoy !!
Awesome flame!
Thanks for sharing!
Wow good job flame!
AND GET THAT MINION
Good article
Posts 1–4 of 4 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us