Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I've been stuck recently trying to generate a strongly typed resource file for my project.

I have multiple ".resx" files and all documentation I've found so far only implies you can only generate a class from a single XML or text file. Or at least that is how I understand the documentation here.

I have inherited a project and have needed to change the namespace, hence the existing Resource.Designer.cs needs to be regenerated.

Thanks in advance for any suggestions, I've been really stuck on this one.

Larry
Posted

1 solution

This is not true at all. You can generate a lot of different strongly-typed resources automatically. You just need to have a file (no, not only a text file, many file types are supported) and give this file proper name. Now, the resource will always be successfully created, but in some cases Visual Studio will generate a strongly typed resource, in other cases, the generated resource will be accessible as an array of bytes. It depends on the file type; if the system is familiar with this type, the generated resource will be strongly typed. If not, you will have to deal with the array of bytes. Typically, you put it in a memory stream and read the stream with your own constructor of your own type which "knows" how to deal with the type of resource in question.

Let me give you an example of, say, PNG image file. You should agree that this is not a text file. You should prepare such file with valid PNG content (most important item, apparently :-)), and name it in a proper way, say, "MyImage.PNG". Create an empty *.resx item, click "Add existing file" (the best option in most question; in particular, try to avoid creation of images in Visual Studio: it will force you using its editor, which is ugly; it won't allow you to edit files using in resources independently). When the file is added, it will add a copy of it to the project (with proper properties, so the file won't be copied to output directory), a reference to this file in the resource file and — attention! — will create an auto-generated C# file.

This auto-generated file will be placed as a child node to your resource file node, as presented in the Solution Explorer. Open this file in the editor. In this file, you will find the static property named MyImage (in some other cases, you will get a property with the name close to the name of your input file). This property will be strongly typed as image. The underlying code will extract it from resource and read and the image object, so you don't need to do anything like that by yourself. Just use this property in your project as some ready-to-use image. When you edit your file "MyImage.PNG" and rebuild your project, the embedded image will be properly updated.

That's all. As to other file types to be uses in resource — please see above.

—SA
 
Share this answer
 
Comments
Laurence1234 6-Aug-13 4:30am    
HI SA,

As always, thanks for your answer - my 5.

In this situation however I have already have the pre-populated ".resx" files. These files contain only strings, translating labels into different languages. I have 4 of these files and rather than re-adding all these entries, I want the static resource class to be automatically generated including the resource entries from all the files (I had to remove the previous version as it was in the wrong namespace).

Using a tool such as RESGEN, I want to pass in all of the 4 files as arguments and the strongly typed class that is generated therefore should include the translations from all 4 resx files.

Sorry perhaps I didn't explain it well enough. Your answer is very useful for the future by the way.
Sergey Alexandrovich Kryukov 6-Aug-13 11:24am    
You are welcome,

If you already have pre-populate .resx with strings, the strongly-typed string properties are also created in the auto-generated C# file, just take a look.

Now, what do you mean "different languages"? Different languages should be in separate projects, to make satellite assemblies. If you use satellite assemblies, correctly named and placed, they are loaded automatically depending on the culture of the thread you set, and automatically reloaded during runtime when you reload the culture during runtime, and fallback to the "closest available" culture if you request the culture which is not localized yes, all automatically. Are you using it all?

—SA
Laurence1234 7-Aug-13 5:10am    
Hi SA,

I've actually solved this problem now. The problem was down to the Default Namespace being incorrectly set in the properties of the .csproj file. Once I set this this and added my resx files, I right clicked on the resx and selected "Run Custom Tool". When I rebuilt my solution the compiler generated a .dll for each resource and placed it into a folder corresponding to the appropriate culture. I assume these are the satellite assemblies you mentioned.

I think I was stuck because I didn't realize if you correctly place your resx into the properties folder of your project and run the custom tool it's actually automating the process of manually using RESGEN then AL.EXE to build the resource file and compile this into the binary satellite assemblies for each culture (or lanaguage to use my previous terminology).
Sergey Alexandrovich Kryukov 7-Aug-13 9:38am    
Great.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900