Click here to Skip to main content
14,825,162 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i will write on Special line of text file
OriginalGriff 28-Dec-13 7:43am
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
We have no idea what a "Special Line" of a text file is, or how you identify it...
Use the "Improve question" widget to edit your question and provide better information.

Thinking about it, do you mean "Specific Line" rather than "Special Line"?
If so, then you may be disappointed.
Text files do not have "lines" as such - they are just a stream of character, where some of the characters are interpreted as line breaks by the software that reads them. You can't just "move to line 102" and write over it, because unless the text you write is exactly the same length as the text you are replacing, it will mess up the file.

If you have organised your text file as equal length lines then you can do it: just use the Seek method to a character position (line number * length of line) then write the correct number of characters, but if you haven't, then you you will have to read from your file up to the start of the line you want to replace, writing as you go to a new file, then skip the text you wish to replace in the original file and write the new text to the output file instead, then copy the rest of the input file to the output. finally, you can close both files, delete the original and rename the new file.

If you really want to deal with row based information, a text file is a poor choice: there are much better solutions out there.
BillWoodruff 28-Dec-13 13:54pm
Dear Mentor, I, a dying flea on a starving tick in the ear of a mangy homeless dog, dare (for once) to quibble with Thee: see below.
OriginalGriff 28-Dec-13 14:16pm
Prithee, quibble as thee wish my friend!
Forsooth without such debate one may come to believe in ones own infallibility. And Herself will inform you (at some considerable length, I warrant) that the rumours of my infallibility are unfounded...
The WinForms TextBox has a 'Lines Property that returns a readonly array of strings. Interestingly, the 'Lines Property, as a whole, can be assigned to.

To modify any one line of Text in a TextBox's Lines array:
private void ModifyLine(TextBox targetTB, int lineToChange, string newLineContent)
    // check the supplied index
    if (lineToChange > targetTB.Lines.Length - 1) 
      throw new ArgumentOutOfRangeException
       "invalid index to Lines collection of TextBox: " + targetTB.Name

    // create a copy of the TextBox's Lines array
    // that can be modified (written to)
    string[] lineAry = new string[targetTB.Lines.Length];
    targetTB.Lines.CopyTo(lineAry, 0);

    // change the specified line's content
    lineAry[lineToChange] = newLineContent;

    // update the target TextBox's Lines array
    targetTB.Lines = lineAry;
A sample of a call to 'ModifyLine:
ModifyLine(textBox1, 2, "modified line 3");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900