1

I would like to iterate through all string resources in a given .NET assembly. To do so I came up with the following code:

public void IterateResourcesInAssembly(string filename)
{
  var assembly = Assembly.LoadFile(filename);
  string[] resourceNames = assembly.GetManifestResourceNames();

  foreach (var resourceName in resourceNames)
  {
    var resourceManager = new ResourceManager(resourceName, assembly);
    var resourceSet = resourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
    // Exception is thrown!
  }
}

The problem here is, that GetResourceSet always throws an exception:

Missing ManifestResourceException For the given culture or the neutral culture no resources could be found...

But I'm pretty sure that's not true. The assembly contains lots of resources in English and German. When opening the Assembly with the .NET-Reflector, I can see those resources, too.

rkhb
  • 14,159
  • 7
  • 32
  • 60
Boris
  • 8,551
  • 25
  • 67
  • 120
  • Have you tried using the method InternalGetResourceSet. I vaguely remember using that method instead to do the same. See. http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager.internalgetresourceset%28v=vs.110%29.aspx. You could also try calling GetNeutralResourcesLanguage first to get the CultureInfo you need to pass in. – stevethethread Nov 11 '14 at 09:11

1 Answers1

4

GetManifestResourceNames() method returns resource name with the extension. Before create resource manager instance, you have to remove the extension from resource name and pass only the resource base name.

public void IterateResourcesInAssembly(string filename)
        {
            var assembly = Assembly.LoadFile(filename);
            string[] resourceNames = assembly.GetManifestResourceNames();

            foreach (var resourceName in resourceNames)
            {
                string baseName = Path.GetFileNameWithoutExtension(resourceName);
                ResourceManager resourceManager = new ResourceManager(baseName, assembly);

                var resourceSet = resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);
                // Exception is thrown!
            }
        }
Chamal
  • 1,439
  • 10
  • 15
  • This works quite well. But it has one problem: no matter what culture I pass in to 'GetResourceSet', I always get the default (English) resources. How could be fixed, so that e.g. 'new CultureInfo("de")' would get me the localized German resources? – Boris Nov 11 '14 at 10:45
  • This is because once the localized resources are introduced it will create a separate satellite assembly to store localized version. For example if you have German localized file, a separate folder will be created "de-DE" in the bin folder it will have the satellite assembly. I assume you are loading the main dll which does not have localized resources. Following link contain a example on how to build assembly path dynamically. http://stackoverflow.com/questions/5246584/c-sharp-cannot-getting-a-string-from-resourcemanager-from-satellite-assembly – Chamal Nov 11 '14 at 11:52