views:

358

answers:

1

I currently use GetManifestResourceStream to access embedded resources. The name of the resource comes from an external source that is not case sensitive. Is there any way to access embedded resources in a case insensitive way?

I would rather not have to name all my embedded resources with only lower case letters.

+3  A: 

Assuming that you know the resource names from the external source and are only lacking the capitalization, this function creates a dictionary you can use for lookups to align the two sets of names.

you know -> externally provided
MyBitmap -> MYBITMAP
myText -> MYTEXT

/// <summary>
/// Get a mapping of known resource names to embedded resource names regardless of capitlalization
/// </summary>
/// <param name="knownResourceNames">Array of known resource names</param>
/// <returns>Dictionary mapping known resource names [key] to embedded resource names [value]</returns>
private Dictionary<string, string> GetEmbeddedResourceMapping(string[] knownResourceNames)
{
   System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
   string[] resources = assembly.GetManifestResourceNames();

   Dictionary<string, string> resourceMap = new Dictionary<string, string>();

   foreach (string resource in resources)
   {
       foreach (string knownResourceName in knownResourceNames)
       {
            if (resource.ToLower().Equals(knownResourceName.ToLower()))
            {
                resourceMap.Add(knownResourceName, resource);
                break; // out of the inner foreach
            }
        }
    }

  return resourceMap;
}
Mark
This function addresses a slightly different scenario than the one I have, however, knowing about the existence of GetManifestResourceNames provides enough information for me to create a solution. Thanks.
Darrel Miller
Glad it helped!
Mark