Reputation: 2379
We have a Res1.resx and it has a file1.doc. Later we would like to replace file1.doc with file2.doc in the resx file based on business rule. How to achieve this. Thanks N
Upvotes: 8
Views: 10876
Reputation: 292345
If you're trying to modify the compiled resource at runtime, I don't think it's possible, at least not without ugly hacks. Anyway, it doesn't make sense: if it's a resource, it's not meant to be modified; and if it needs to be modified, it shouldn't be a resource.
Upvotes: 2
Reputation: 1016
Sure you can do that. Here's a bit of code we use. Please let me know if you need more details.
/// <summary>
/// Sets or replaces the ResourceDictionary by dynamically loading
/// a Localization ResourceDictionary from the file path passed in.
/// </summary>
/// <param name="resourceDictionaryFile">The resource dictionary to use to set/replace
/// the ResourceDictionary.</param>
private void SetCultureResourceDictionary(String resourceDictionaryFile)
{
// Scan all resource dictionaries and remove, if it is string resource distionary
for ( int index= 0; index < Resources.MergedDictionaries.Count; ++index)
{
// Look for an indicator in the resource file that indicates that this
// is a dynamic file that should be removed before loading its replacement.
if (Resources.MergedDictionaries[index].Contains("ResourceDictionaryName"))
{
if ( File.Exists(resourceDictionaryFile) )
{
Resources.MergedDictionaries.Remove(Resources.MergedDictionaries[index]);
}
}
}
// read required resource file to resource dictionary and add to MergedDictionaries collection
ResourceDictionary newResourceDictionary = new ResourceDictionary();
newResourceDictionary.Source = new Uri(resourceDictionaryFile);
Resources.MergedDictionaries.Add(newResourceDictionary);
}
Upvotes: 2
Reputation: 2262
you should have a look at ResXResourceWriter and ResXResourceReader
Upvotes: 5