tags:

views:

1274

answers:

2

Is there anyway to determine if a ResourceManager contains a named resource? Currently I am catching the MissingManifestResourceException but I hate having to use Exceptions for non-exceptional situations. There must be some way to enumerate the name value pairs of a ResourceManager through reflection, or something?

EDIT: A little more detail. The resources are not in executing assembly, however the ResourceManager is working just fine. If I try _resourceMan.GetResourceSet(_defaultCuture, false, true) I get null, whereas if I try _resourceMan.GetString("StringExists") I get a string back.

+1  A: 

I think you can use something like Assembly.GetManifestResourceNames to enumerate the list of resources available in the Assembly's manifest. It isn't pretty and doesn't solve all of the corner cases, but works if required.

sixlettervariables
That returns an array of zero strings.
Kris Erickson
Are the resources in a satellite resource?
sixlettervariables
You are giving him ResourceManager names, not names of resources in those ResourceManagers ;).
Jonathan C Dickinson
Interesting, I had used this to get resources along side an assembly, rather than resource managers. i.e. Project\Resources\Image.jpg -> Assembly.GetManifestresourceNames would return Project.Resources.Image.jpg. Is this not what he wanted?
sixlettervariables
Yes the resources are in a satellite resource.
Kris Erickson
In that case you'd need to use the Assembly.GetManifestResourceNames on the satellite assembly. The ResourceSet solution works as well, just can be expensive if you don't want to load everything.
sixlettervariables
+2  A: 

You can use the ResourceSet to do that, only it loads all the data into memory if you enumerate it. Here y'go:

    // At startup.
    ResourceManager mgr = Resources.ResourceManager;
    List<string> keys = new List<string>();

    ResourceSet set = mgr.GetResourceSet(CultureInfo.CurrentCulture, true, true);
    foreach (DictionaryEntry o in set)
    {
        keys.Add((string)o.Key);
    }
    mgr.ReleaseAllResources();

    Console.WriteLine(Resources.A);
Jonathan C Dickinson
When I try to get the ResourceSet from the ResourceManager I get null back.
Kris Erickson
Ok, I got it working, but I have to try to load a single resource before I can call GetResourceSet. Wierd.
Kris Erickson