How I can open an existing .resx file, write some resources and close without losing the resources that were in the original file?
Asked
Active
Viewed 2,436 times
4
-
Isn't that the default behavior already? I don't usually lose any existing resources when editing .resx files. Are you not using Visual Studio as your editor? – Klaus Byskov Pedersen Feb 21 '11 at 12:42
-
possible duplicate of [Modifying .resx file in c#](http://stackoverflow.com/questions/676312/modifying-resx-file-in-c) – Klaus Byskov Pedersen Feb 21 '11 at 12:45
-
1I'm using ResXResourceWriter for resource writing – Javier Marín Feb 21 '11 at 14:09
-
It is possible (although maybe not the best technique) to work on a Resources.resx file as an XML document. http://stackoverflow.com/a/43431478/253938 – RenniePet Apr 15 '17 at 21:38
1 Answers
9
The ResXResourceWriter just writes the new nodes.. To keep the old ones you can do like this : The code below will add one new node to the resx file and by iterating the old nodes writing them again. I bet there is a more simple way in doing this, but can't figure it out.
var reader = new ResXResourceReader("filename");
var node = reader.GetEnumerator();
var writer = new ResXResourceWriter("filename");
while (node.MoveNext())
{
writer.AddResource(node.Key.ToString(), node.Value.ToString());
}
var newNode = new ResXDataNode("name", "value");
writer.AddResource(newNode);
writer.Generate();
writer.Close();
Remember to add the using System.Resources

Jonas W
- 3,200
- 1
- 31
- 44