gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:17:32 24 4
gpt4 key购买 nike

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

我宁愿不必只用小写字母命名所有嵌入的资源。

最佳答案

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

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;
}

关于.net - 在 .Net 中使用不区分大小写的文件名检索嵌入资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/978736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com