简体   繁体   中英

Add data at the end of existing data in resource file

I want to add data at the end of existing data in resource file on the click of button in windows form. I have a windows form with 3 text boxes -

text_box1: Name
text_box2: Value
text_box3: Comments
and a button named as Save.

code for button:

 private void button1_Click(object sender, EventArgs e)
        {
            myMethod.Create(textBox1.Text, textBox2.Text, textBox3.Text);
        }

Method to add data in resource file:

public class myMethod
    {
        public static void Create(string myName, string myValue, string myComment)
        {
            try
            {
                ResXResourceReader resxReader = new ResXResourceReader(@"D:\Validator_Tool\resx\resx\myres.resx");
                resxReader.UseResXDataNodes = true;
                IDictionaryEnumerator data = resxReader.GetEnumerator();

            while (data.MoveNext())
            {
                ResXResourceWriter resxWriter = new ResXResourceWriter(@"D:\Validator_Tool\resx\resx\myres.resx");
                var node = new ResXDataNode(myName, myValue);
                node.Comment = myValue;
                resxWriter.AddResource(node);
                resxWriter.Close();
            }

            }
            catch (FileNotFoundException caught)
            {
                MessageBox.Show("Source: " + caught.Source + " Message: " + caught.Message);
            }
        }
    }

Please help me in this problem to add data in resource file because with this code my data is overwrite with existing data but I want my data should append at the end of the existing data. This code giving me exception too while using IDictionaryEnumerator exception is:

System.InvalidOperationException

This is a snippet, to add a new line to your resx and keep the old records:

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();

This will re-write your old lines and add your new line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM