4

現在、GetManifestResourceStreamを使用して埋め込みリソースにアクセスしています。リソースの名前は、大文字と小文字を区別しない外部ソースから取得されます。大文字と小文字を区別しない方法で埋め込みリソースにアクセスする方法はありますか?

埋め込まれたすべてのリソースに小文字だけで名前を付ける必要はありません。

4

3 に答える 3

14

外部ソースからのリソース名を知っていて、大文字化が欠けているだけであると仮定すると、この関数は、ルックアップに使用して 2 つの名前のセットを揃えることができる辞書を作成します。

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;
}
于 2009-06-11T01:02:57.400 に答える
0

上記の受け入れられた回答と同様の回答ですが、VB.NET でソリューションを共有したかったのです。

''' <summary>
''' Gets the file name in its proper case as found in the assembly.
''' </summary>
''' <param name="fileName">The filename to check</param>
''' <param name="assemblyNamespace">The namespace name, such as Prism.Common</param>
''' <param name="assembly">The assembly to search</param>
''' <returns>The file name as found in the assembly in its proper case, otherwise just filename as it is passed in.</returns>
''' <remarks></remarks>
Public Shared Function GetProperFileNameCaseInAssembly(ByVal fileName As String, ByVal assemblyNamespace As String, ByVal assembly As System.Reflection.Assembly) As String
    For Each resource As String In assembly.GetManifestResourceNames()
        'Perform a case insensitive search to get the correct casing for the filename
        If (resource.ToLower().Equals(String.Format("{0}.{1}", assemblyNamespace, fileName).ToLower())) Then
            'cut off the namespace assembly name in the resource (length + 1 to include the ".") to return the file name
            Return resource.Substring(assemblyNamespace.Length + 1)
        End If
    Next
    Return fileName 'couldn't find the equivalent, so just return the same file name passed in
End Function
于 2014-03-27T14:36:49.703 に答える