How can I change resource files for a c# winform a

2019-07-03 18:18发布

问题:

We have a winform app that has been in development for some time. It has several images and control colours that create a branded experience. We have a second customer who would like the same product, but with different branding.

So we have lines of code in the control designer.cs files like:

this.BackgroundImage = global::MyNameSpace.Properties.Resources.background;

It seems like resource files are the way to go, as I could simply change the culture and have a separate resource file with different images.

But I don't want to change the culture (I don't want the display of dates, currency, etc.) to be altered and it just smells.

Can I change the resource file manually at runtime or compile time without having to change the culture?

Is there a better way to achieve this outcome?

Also, how do you / can you use resource files to set controls colours (e.g. change all backgrounds from black to red)?

回答1:

You can read in the resources from a separate file that just had the resources (but doesn't depend on the globalized satellite assembly mechanism). You could also compile default resources with your main assembly as a fall-back.

You can use Resgen.exe to create a .resources file from, e.g., a .resx file and then use ResourceManager to read it:

//note that resourceFilename does NOT include the ".resources" extension
var rm = ResourceManager.CreateFileBasedResourceManager( resourceFilename
                                                        ,resourceDir
                                                        ,null);
this.BackgroundImage = (Image) rm.GetObject("background");


回答2:

One option you could consider is to create a custom culture, so for example you could create a culture called

ClientA-fr-FR
ClientB-fr-FR

The you create resources for each and you should be able to set the Thread Culture approriately.

Here is a link "How to: Create Custom Cultures"