Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a resource exists in ResourceManager

Tags:

c#

resources

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.

like image 527
Kris Erickson Avatar asked Oct 01 '08 22:10

Kris Erickson


2 Answers

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);
like image 145
Jonathan C Dickinson Avatar answered Oct 15 '22 15:10

Jonathan C Dickinson


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.

like image 3
user7116 Avatar answered Oct 15 '22 16:10

user7116