ARTICLE

Handle resource files - Use resgen.exe to generate resources files: Part V

Posted by Bechir Bejaoui Articles | Visual C# June 02, 2008
The resgen.exe is a wonderful tool provided by the .Net framework to generate resx, resources files. In this article I wil show the trick of how to use it in .Net.
Reader Level:

The resgen.exe is a wonderful tool provided by the .Net framework to generate

a from
Resource file Resx file
Resx file Resource file
Resource file Text file(*not recommended, I'll explain why later in this article)
Text file Resource file (*not recommended, I'll explain why later in this article)

The resgen.exe name is an abbreviation of "Resource file generator". This executable generates files from and to each other format by wrapping the ResXResourceWriter, ResXResourceReader, ResourceWriter and ResourceReader classes' methods.  

Note that in order to use this tool correctly, files must be written exactly in right format, I meant you have to respect the contents structuring roles according to each input file type. Otherwise, the resgen.exe will not generate the out put file and will return -1 as result. Other remark should be kept in mind; the files' names shouldn't contain spaces or special characters, if it is the case. The resource file generator will replace any unreadable character or space by an underscore.

To better understand how to use resgen.exe, I propose those walkthroughs:

Generate a .resource file from a .resx file

Suppose that we have a .resx file named 'C: \myResxFile.resx', if you don't have one, select Start>research> all files and repertories, and then write *.resx in the textbox and press scan. Select any resx file then copy and paste it in the "C: \ "repertory. You can rename it 'C: \myResxFile.resx' if you want.

The purpose here is to generate a resource file from it. The resource file is used in general to be embedded within the executable file, remember that the resource format file is a binary file. Once it is generated, you can not directly open and modify it easily. So first of all, verify the resx file structure by opening it with a note pad and I suggest that you use Notepad ++ witch is an excellent freeware editor that supports the majority of the programming languages including the resources files, then verify if the given file name is containing spaces or special characters and replace them by valid characters if it's the case.

Now, open the Framework SDK by selecting all programs>Microsoft .Net Framework SDK v2.0>SDK Command prompt. Then execute the command as follow

res1.gif
 
Figure 1

Browse to "C:\" repertory, you will find the new generated file there 'C: \myResourceFile.resources. Note that if you put only the input file as command parameter, the resgen.exe will generate a 'resources' file with the same name as the input file in the same directory.

Generate a .resx file from a .resource file

Some times we need to process in the opposite direction. I mean, generate a resx file from a resource file. You know, if you already have a resource file witch is a binary file, and you want to put some modifications on it. It is not easy to edit it directly and perform your transformation. In this case, you process as follow. As a first step, you should use the resgen.exe to generate a resx file from a resources file, and then edit it, perform your modifications, and then save them. The second step is that you use the resgen.exe another once to regenerate the new resources file version. Finally, you replace the old version by the new one.

Suppose that we have a .resources file named 'C: \myResourcesFile.resources', if you don't have one, select Start>research> all files and repertories, and then write *.resources in the textbox and press scan. Select any resources file then put it in the "C: \" repertory. You can rename it 'C: \myResourcesFile.resources.

Now, open the Framework SDK by selecting all programs>Microsoft .Net Framework SDK v2.0>SDK Command prompt. Then execute the command as follow

res2.gif
 
Figure 2

Browse to "C:\" repertory, you will find the new generated file there 'C: \myResxFile.resx'. Note that if you put only the input file as command parameter, the resgen.exe will generate a 'resx' file with the same name as the input file in the same directory.

Generate a .resources file from a .text file

To generate a .resources file from a text file correctly, you have to follow those steps. First of all, define a new txt file and add some elements into it. The elements should be strings and not objects * because they are represented as binary entities those can lost some information during the generation process. Strings have to be disposed name = value like Town = Bejaia. You can add comments to explain in advantage the attributes, but you have to precede each comment by a semi colon ";" such "; this is … ". Unfortunately those comments will be lost during the generation process. According to numbers, they must be preceded by "#" character. You can add as many as you like in terms of resources because there are no limits in terms of files size. Empty attributes are permitted like name =, duplicated attributes, I mean attributes with same names, are automatically omitted and the resgen.exe generates a warning that there are duplicated attributes, however. To represent a new line you can use \n and to represent a new tab you can use \t such in C/C++ programming language. 

For example, this is a sample how elements should be disposed:

; My first name
FirstName = Bechir
; My last name
LastName = Bejaoui
; My region
Region = North Africa
; My education
Level = Master degree
; My profession
Profession = Freelance

Save those contents as C:\myResourcesTextFiles.txt. Then, open the Framework SDK by selecting all programs>Microsoft .Net Framework SDK v2.0>SDK Command prompt. Then execute the command as follow

res3.gif
 
Figure 3

Finally, browse to the "C:\ " repertory. You find the new generated file there.

Generate a .text file from a .resources file

Another way to explore the resources files contents is by generating a text file from it. Suppose that we have a .resources file named 'C: \myResourcesFile.resources', if you don't have one, select Start>research> all files and repertories, and then write *.resources in the textbox and press scan. Select any resources file then put it in the C: \ repertory, you can rename it 'C: \myResourcesFile.resources. Note that resource file should contain only strings attribute because in the majority of cases when a resources file contains objects, the resgen.exe fails to generate a txt file and a null reference exception will be thrown.

Now, open the Framework SDK by selecting all programs>Microsoft .Net Framework SDK v2.0>SDK Command prompt. Then execute the command as follow.

res4.gif
 
Figure 4
 
Finally, browse to the "C:\" repertory and you will find the new text generated file there.

Good dotneting!!!

COMMENT USING
Employers - Post Free Jobs