无法在C#应用程序中使用非本地化资源
如中所述,我有一组字符串资源,这些资源在程序的生命周期内不会改变,但只能通过对字符串列表进行昂贵的操作来获得:无法在C#应用程序中使用非本地化资源,c#,resourcemanager,C#,Resourcemanager,如中所述,我有一组字符串资源,这些资源在程序的生命周期内不会改变,但只能通过对字符串列表进行昂贵的操作来获得: public static class Patterns { Public static readonly string StateUS = @"\b(?<STATE>" + CharTree.GenerateRegex(FixedLists.StateUS) + @")\b"; Public static readonly
public static class Patterns
{
Public static readonly string StateUS =
@"\b(?<STATE>" + CharTree.GenerateRegex(FixedLists.StateUS) + @")\b";
Public static readonly string Countries =
@"\b(?<STATE>" + CharTree.GenerateRegex(FixedLists.Countries) + @")\b";
//.....
}
然后编译此文件并将其复制到生成后的目标目录:
cd $(ProjectDir)
"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\resgen.exe" /compile Regex.restext
copy /Y Regex.resources "$(TargetDir)MyProject.Resources.Lists.resources"
但是当我尝试在模式的静态构造函数中加载这些值时:
ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("MyProject.Resources.Lists", @".\", null);
StateUS = rm.GetString("StateUS")
我得到以下错误:
InnerException: System.Resources.MissingManifestResourceException
Message=Could not find any resources appropriate for the specified culture (or the neutral culture) on disk.
System.Resources.FileBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark)
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark)
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
但这是一个文化中立的项目——资源只是用于存储。那么,为什么我需要指定一种文化呢?我将如何/在哪里这样做
如果我在加载资源时采用了错误的方法,那么有什么更好的方法呢?此异常告诉您找不到MyProject.resources.Lists.resources文件。可能文件路径或文件名本身不正确。我通过创建一个示例应用程序验证了您的示例,效果很好
您应该将@“\”
替换为Path.GetDirectoryName(Assembly.getExecutionGassembly().Location
,以确保资源文件使用正确的位置。如果您在应用程序中的任何位置设置目录.SetCurrentDirectory(“D:\”)
会在那里查找您的资源。如果它是静态的,为什么不生成一次,然后复制并粘贴到您的源代码中?因为基础列表在软件版本之间经常更改。是的……我在测试资源管理器中运行了一个NUnit测试,它在不同的文件夹中运行。显然,依赖于编译后复制不起作用。
InnerException: System.Resources.MissingManifestResourceException
Message=Could not find any resources appropriate for the specified culture (or the neutral culture) on disk.
System.Resources.FileBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark)
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark)
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)