使用.Net中的不区分大小写的文件名检索嵌入的资源

Dar*_*ler 4 .net embedded-resource

我目前使用GetManifestResourceStream来访问嵌入式资源.资源的名称来自不区分大小写的外部源.有没有办法以不区分大小写的方式访问嵌入式资源?

我宁愿不必仅使用小写字母来命名所有嵌入式资源.

Mar*_*ark 11

假设您知道来自外部源的资源名称并且仅缺少大小写,则此函数会创建一个字典,您可以使用该字典进行查找以对齐两组名称.

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;
}
Run Code Online (Sandbox Code Playgroud)