Working with “resx” resources
September 4, 2011 at 7:55 am Leave a comment
Hello folks,
Recently I had a task to load dynamically resx (the WinForms) resources. The idea was to discover all embedded resources in any of product assemblies. So, I used “GetManifestResourceNames” method to get resources names from inspected assembly, and after that created a ResourceManager instance managing these resources:
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly searchAssembly in assemblies)
{
string[] resourceNames = searchAssembly.GetManifestResourceNames();
foreach (var resourceName in resourceNames)
{
ResourceManager manager = new ResourceManager(resourceName , searchAssembly);
}
}
The initialization of ResourceManager passed successfully, however trying to get any string from it was resulting in following exception:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure “MySolution.g.resources.resources” was correctly embedded or linked into assembly “MySolution” at compile time, or that all the satellite assemblies required are loadable and fully signed.
It took me a while to discover, that the resource name as it returned by “GetManifestResourceNames” method is not good for ResourceManager.
In order to make it work, the postfix “.resources” has to be removed. The following line of code fixes the problem:
ResourceManager manager = new ResourceManager(Path.GetFileNameWithoutExtension(resourceName), searchAssembly);
Cheers,
Evgeni
Entry filed under: General C#. Tags: c#, resources, resx.
Trackback this post | Subscribe to the comments via RSS Feed