I have a requirement to write and edit in Resource file en_US.resx which were being used in Globalization. below is my code.
System.Reflection.Assembly ass = System.Reflection.Assembly.Load("App_GlobalResources");
ResourceManager resourcer = new ResourceManager("en_US.resx", ass);
ResourceWriter writer = new ResourceWriter("resourcer");
writer.AddResource("Name", "new user");
writer.Close();
jvak1981
Member
12 Points
9 Posts
writing and Editing Resource .resx files in asp.net 3.5 c#
Nov 10, 2011 08:31 AM|LINK
Hi,
I have a requirement to write and edit in Resource file en_US.resx which were being used in Globalization. below is my code.
System.Reflection.Assembly ass = System.Reflection.Assembly.Load("App_GlobalResources"); ResourceManager resourcer = new ResourceManager("en_US.resx", ass); ResourceWriter writer = new ResourceWriter("resourcer"); writer.AddResource("Name", "new user"); writer.Close();can u help me on this.
thanks in Advance,
AnilJayanti
shashankgwl
All-Star
18926 Points
3662 Posts
Re: writing and Editing Resource .resx files in asp.net 3.5 c#
Nov 10, 2011 09:13 AM|LINK
what problem do u face?
All is well if it runs well.
blog
jvak1981
Member
12 Points
9 Posts
Re: writing and Editing Resource .resx files in asp.net 3.5 c#
Nov 10, 2011 09:17 AM|LINK
Hi,
thanks ,
After executing above code, data "New User" not been added to en_US.resx resource file. I just want to add my dynamic data to resx file.
Aniljayanti
jvak1981
Member
12 Points
9 Posts
Re: writing and Editing Resource .resx files in asp.net 3.5 c#
Nov 11, 2011 03:06 AM|LINK
Hi All,
Got solution for this.. giving below my code.
string sResxPath = @"E:\testApp\App_GlobalResources\en_US.resx"; Hashtable data = new Hashtable(); data.Add("name", "sunil"); UpdateResourceFile(data, sResxPath); public static void UpdateResourceFile(Hashtable data, String path) { Hashtable resourceEntries = new Hashtable(); //Get existing resources ResXResourceReader reader = new ResXResourceReader(path); if (reader != null) { IDictionaryEnumerator id = reader.GetEnumerator(); foreach (DictionaryEntry d in reader) { if (d.Value == null) resourceEntries.Add(d.Key.ToString(), ""); else resourceEntries.Add(d.Key.ToString(), d.Value.ToString()); } reader.Close(); } //Modify resources here... foreach (String key in data.Keys) { if (!resourceEntries.ContainsKey(key)) { String value = data[key].ToString(); if (value == null) value = ""; resourceEntries.Add(key, value); } else { String value = data[key].ToString(); if (value == null) value = ""; resourceEntries.Remove(key); resourceEntries.Add(key, data[key].ToString()); } } //Write the combined resource file ResXResourceWriter resourceWriter = new ResXResourceWriter(path); foreach (String key in resourceEntries.Keys) { resourceWriter.AddResource(key, resourceEntries[key]); } resourceWriter.Generate(); resourceWriter.Close(); }