Plain Text Return

Using the return character "\n" or "\r", and saving it to a text file, when opened in notepad, the return does not show, but it shows in rich text editors. Is there a return character in .net that works in notepad

Answer this question

Plain Text Return

  • Venom

    For some weird reason, you're right, \n is not properly saved on a TextFile. The way I save multi-lines of text is I split the string into lines, using \n as my delimiter:

    string test = "Test\nTest in line2";
    string [] lines = test.Split('\n');


    And then, if I work on a TextWriter, I usually iterate through the lines of strings, and use the WriteLine() method instead of Write(), here's a code to show my method:

    string test = "Test\nTest in line2";
    string [] lines = test.Split('\n');
       
    TextWriter tw = new StreamWriter(@"c:/test.txt");
       
     foreach(string line in lines)
     {
      tw.WriteLine(line);
     }

    tw.Close();

    // To display it on a multi-line TextBox:
    textBox1.Lines = lines;



    Maybe other people here can give us a better approach.


    -chris



  • aabes

    Try using "Environment.NewLine". This post is not in the correct forum though.
  • Game Lover Jon

    Thanks for your answers. I used the Environment::NewLine and it works for me. Splitting the textbox into an array was also a good idea.

    Thanks
    -Kyro

  • Plain Text Return