views:

1498

answers:

3

In my C# application I need to create a resx file of strings customized for every customer. What I want to do is avoid to recompile the entire project every time I have to provide my application to my customer, so I need to dynamic access to this string. So, how can I access (during the app execution) to a resx file if I kwow the file name only on the execution time?

Since now I write something similar:

Properties.Resources.MyString1

where Resource is my Resource.resx file. But I need something like this:

GetStringFromDynamicResourceFile("MyFile.resx", "MyString1");

Is it possible?

Thanks Mark

+1  A: 

Of course it is possible. You need to read about ResouceSet class in msdn. And if you want to load .resx files directly, you can use ResxResourceSet.

arbiter
+4  A: 

Will something like this help in your case?

Dictionary<string, string> resourceMap = new Dictionary<string, string>();

public static void Func(string fileName)
{
    ResXResourceReader rsxr = new ResXResourceReader(fileName);        
    foreach (DictionaryEntry d in rsxr)
    {
        resourceMap.Add(d.Key.ToString(),d.Value.ToString());           
    }        
    rsxr.Close();
}

public string GetResource(string resourceId)
{
    return resourceMap[resourceId];
}
Prashanth
It is better to wrap ResXResourceReader into **using** statement, instead of explicitly closing reader.
arbiter
very nice code.
Space Cracker
A: 

You could put the needed resources into a separate DLL (one for each customer), then extract the resources dynamically using Reflection:

Assembly ass = Assembly.LoadFromFile("customer1.dll");
string s = ass.GetManifestResource("string1");

I may have the syntax wrong - it's early. One potential caveat here: accessing a DLL through Reflection will lock the DLL file for a length of time, which may block you from updating or replacing the DLL on the client's machine.

MusiGenesis