Reputation: 4878
I have created a resource file using File -> New item > Resource and put 2 keys and 2 values.
If i set it as embedded resource then i can do and receive the value:
ResourceName.ResourceManager.GetString("Key");
But when i do it when resource is not embedded, i get exception.
A first chance exception of type
'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll
How can i create ResourceManager object that initialized itself with the resource i created ?
I tried:
ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager
(resourceFile, filePath, null);
when path and filename were ok but the object was null and threw exception.
I was following this read value from source.. but did not work either. file could not be loaded ?
Upvotes: 1
Views: 967
Reputation: 14537
To answer the question that ilansch added in the comments to the original question:
I would also like to have a member i can call and search for keys
here's one way (assuming you've already added a "Resources File" called "ResourceName.resx" to your project):
ResourceSet rs = ResourceName.ResourceManager.GetResourceSet(
Thread.CurrentThread.CurrentUICulture, true, true);
Dictionary<string, object> resources = rs.Cast<DictionaryEntry>().ToDictionary(r => (string) r.Key, r => r.Value);
// Can now list all keys, e.g.:
foreach (string key in resources.Keys)
{
Debug.WriteLine(key);
}
// ...or can find out whether a key is present:
object v;
if (resources.TryGetValue("Key", out v))
{
Debug.WriteLine(v);
}
That loads all of the resources in the resource file into a Dictionary<string, object>
, and you can then use that to discover keys. This may not be massively efficient though, as it will load the entire set of resources into memory.
If you just want a list of keys, you can do this:
string[] keys = rs.Cast<DictionaryEntry>().Select(r => (string) r.Key).ToArray();
Not sure if that actually helps, because it's not clear to me what your overall goal is here.
Upvotes: 2