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

Advertisement

Entry filed under: General C#. Tags: , , .

Lecture 3: WPF architecture Overcoming the limitation of having MultiBinding inside another MultiBinding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories


Follow

Get every new post delivered to your Inbox.