Question

As mentioned in a previous question, I have a set of string resources that do not change during the lifetime of the program, but can only be obtained through an expensive operation over a list of strings:

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

    //.....
}

In an attempt to improve performance, I have generated the desired string with T4, which reads lists from a file and outputs a resource file Regex.restext:

StatesUS=A(LA(SK|BAM)A|RKANSAS)|B.....
Countries=UNITED (STATES|KINGDOM)|CA(NAD|MBODI)A.....

This file is then compiled copied to the target-directory post-build:

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"

But when I attempt to load these values within the static constructor for Patterns:

ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("MyProject.Resources.Lists", @".\", null);
StateUS = rm.GetString("StateUS")

I get the following error:

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)

But this is a culture-neutral project - the resources are just for storage. So why do I need to specify a culture at all? How/where would I do so?

And if I'm taking the wrong approach to loading my resources, what would be a better way to do it?

Était-ce utile?

La solution

This exception tells you that the MyProject.Resources.Lists.resources file was not found. Maybe the path to the file or the filename itself is incorrect. I validated your example by creating a sample app and it works fine.

You should replace the @".\" with Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location to ensure you are using the right location for your resource file. In the case you are setting Directory.SetCurrentDirectory("D:\\"); anywhere inside your application, the @".\" would look there for your resources.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top