-2

I have some data and I want to write them to a specific line in notepad using C#.

For example I have two textboxes and the data inside them are "123 Hello", for textBox1, and "565878 Hello2" for textBox2.

When I press SAVE button, those data will be saved into one file but with different line. I want to save the first data in the first line and the second data in the third line.

How can I do this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Well you should look for the answer on the internet rather asking - 'new line text file edit C#' that's all http://stackoverflow.com/questions/8255533/how-to-add-new-line-into-txt-file – singhsac Nov 03 '15 at 03:33

2 Answers2

1

This question is too broad. The simple answer is that you write the two lines to a file, but write a newline (either "\r\n" or Environment.NewLine) between each string. That will put the two strings on different lines. If you want the second string on the third line, then you should write two newlines between each string.

If neither of those are the answer, then you need to be a lot more specific about why not. Is the file empty to start with? What have you tried? Where, specifically, are you getting stuck? What platform?

And I really don't see what this has to do with NotePad.

EDIT:

You have clarified that you are starting with an existing text file and want to replace the content at the specified lines.

This is a more complex thing to do, and may be beyond your skills if you are just starting out. The basic approach is this:

Assuming you can read the entire file into memory, load the file into a string. You will have to parse new lines to find the lines you want to replace. You can then just replace those parts of the string with the new data. When finished, write the file back to disk.

If the file is too big to load into memory, then it becomes much more complex. I'm sorry, but since you've done such a poor job of describing the issue, I'm not going to the trouble of going over the details for this case. And such a task probably falls outside the scope of a stackoverflow answer any way.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • I'm so sorry. I mean when I want to save another data from textBox1 and textBox2, those data will be saved in the same line like the old data (overwrite the old data). Data in textBox1 will be saved in first line and data in textBox2 will be saved in the second line. What must I do ? – Alfonsus Dhani Nov 03 '15 at 03:42
0

If you line numbers are not fixed you can do something like below:

class Program
    {
        private static void Main()
        {
            var data = "";
            const string data1 = "Data1";//First Data
            const string data2 = "Data2";//Second Data
            const int line1 = 1;//First Data Line
            const int line2 = 3;//Second Data Line
            var maxNoOfLines = Math.Max(line1, line2);
            for (var i = 1; i <= maxNoOfLines; i++)
            {
                if (i == line1)
                {
                    data += data1 + Environment.NewLine;
                }
                else if (i == line2)
                {
                    data += data2 + Environment.NewLine;
                }
                else
                {
                    data += Environment.NewLine;
                }
            }
            File.WriteAllText(@"C:\NOBACKUP\test.txt", data);
        }
    }

Otherwise if line numbers are fixed it will be much more simpler. You can just remove the loop from above and hardcode the values.

ATP
  • 553
  • 1
  • 3
  • 16