So I have a ResourceManager that points to a resource file with a bunch of strings in it. When I call GetString()
with a key that doesn't exist in the file, I get a System.Resources.MissingManifestResourceException
. I need to find out whether the Resource contains the specified key without using exception handling to control program flow. Is there a keys.exists() method or something?
views:
172answers:
3Not sure what the problem was, but I got frustrated and deleted/recreated the resx files and this worked.
Calling the GetString method with a key that doesn't exist does not raise an exception, it just returns null.
However, the MissingManifestResourceException occurrs when trying to create a ResourceManager with the wrong name. The most common error is to forget to include the namespace in the name of the resources.
For example, doing :
var r = new ResourceManager("MyResource", assembly);
instead of
var r = new ResourceManager("MyNamespace.MyResource", assembly);
will result in a MissingManifestResourceException.
Note that by default, it appears that a new .net project's Resources.resx is going to be in the Properties folder, so you'll need to create the ResourceManager like this:
rm = new ResourceManager("MyNamespace.Properties.MyResource", assembly);
Alternatively, by getting frustrated and deleting/recreating Resources.resx, you'll probably create it in the root of the project, in which case the thing you were doing before, namely this:
rm = new ResourceManager("MyNamespace.MyResource", assembly);
will work. This is exactly what happened to me today, and I'm adding this post in the hope that it will spare someone some grief.