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!
-Rest in peace leechers-
Your PM box is 100% full.
Hassan (06-04-2010)
Wow good job flame!
AND GET THAT MINION
Hassan (06-04-2010)
Good article
Hassan (06-04-2010)