2

I want to write a string into a file. My problem is that I cannot add new lines into the file.

using (System.IO.StreamWriter writer = new System.IO.StreamWriter(String.Format("{0}.txt", svDialog.FileName)))
            {
                writer.Write(String.Format("{0}\n\n{1}", this.currentRealWorldObj.Title, this.currentRealWorldObj.Description));
            }

As you can see I am adding to 2 new lines ("\n\n") in the String.Format method. But when I open the lines are not added. How they can be added?

1
  • try File.WriteAllText(fileName, textToWrite); Commented Aug 28, 2016 at 18:49

2 Answers 2

8

There is a specific WriteLine function for this.

For example:

writer.WriteLine(this.currentRealWorldObj.Title);
writer.WriteLine();
writer.WriteLine();
writer.WriteLine(this.currentRealWorldObj.Description);
7

Maybe platform issues? Look at Difference between "\n" and Environment.NewLine Try Environment.NewLine

writer.Write(String.Format("{0}{2}{2}{1}", this.currentRealWorldObj.Title, this.currentRealWorldObj.Description,Environment.NewLine));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.