Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:

I need to write multiple line string to file.
I have readed succesfuly lines to string from textfile using
string text = sr.ReadToEnd();

Then I've loaded that string to textbox.
What I want now is to modify multiple line text in textbox and save it to file.
How to do it?

Thanks for any kind of help.

How about:

string filepath = ...;
string text = File.ReadAllText(filepath);
File.WriteAllText(filepath, text);

Is this what you ask for?
The text already contains the new lines, while reading and writing - no special handling needed, right?
Share this answer
In addition to the Solution 3 mention, you could try using one of the following methods,

WriteAllLines(String, IEnumerable(Of String))
WriteAllLines(String, String())
WriteAllLines(String, IEnumerable(Of String), Encoding)
WriteAllLines(String, String(), Encoding)
WriteAllText(String, String)
WriteAllText(String, String, Encoding)

More details File Class[^]

Hope it helps :)
Share this answer
An important addition to other answers: don't use explicit

You should never use "\r\n" — using immediate constants is bad; and this is not portable. Right thing is using static property System.Environment.NewLine.

(By the way, you really should avoid all those immediate constants by all means, except something like 0, 1, null. Even the string "" is bad; you should use string.Empty.)

Please see:[^].

Share this answer
Hi there,

You can set the Text Box multi-line property to true so that your text box can display multiple lines/edit multiple line - this would allow the user (you?) to edit the text in the program. If you wish to edit multi-line text dynamically in code use the escape characters \r and \n to create new lines. \r is the reset character and \n is the new line character. Usage example:

static string AppendNewLine(string Input)
    return Input + "\r\n";

This code will add a new line to the end of the string. You can add multiple of these in a row to achieve multiple new (blank) lines i.e. don't be worried about calling this method twice in a row - the effect remains the same.

Hope this helps,
Share this answer
LosEagle 21-Feb-12 15:10pm    
I have TextBox multiline property set to true and I've already readed file to textbox and changed the text. I've made button which will read the textbox and save it to string after the click. But I just don't know how to write that string into a file :(. Didn't found any command for that. Just Writeline() and it will writes all the things in the string to one line I think. But still thanks for all the things you wrote.
Ed Nutting 21-Feb-12 15:13pm    
Just use .Write - the text box gives you the new line characters as it is multi-line - basically you don't need to do anything special - it's wonderful what C# does for you :) Just do .Write(MyMultiLineBox.Text) and it'll do what you want :)
Sergey Alexandrovich Kryukov 26-Feb-12 13:25pm    
You should never use "\r\n" -- using immediate constants is bad; and this is not portable. Right thing is using static read-only property System.Environment.NewLine.
Ed Nutting 26-Feb-12 13:31pm    
I have never found an issue with this across any of Windows, Mac or Linux - why do you think this isn't portable? Even the Microsoft MSDN stuff uses hard-coded new line characters for stuff... Not seeing the advantage of System.Environment.NewLine unless your in some very odd situation...

Edit: Immediate constants can be bad practice I will agree but I do not agree in this specific (and special) case.
Sergey Alexandrovich Kryukov 26-Feb-12 14:00pm    
Because I know it. And you apparently not. Write a file and compare it with a "natively written" file on Linux and Mac.
"...unless in some very odd situation" is a pure lie. The right thing is always "System.Environment.NewLine".

You behavior in order to protect your wrong advice by all means even in obvious situation is very counter-productive. I'll vote 1, because it means making harm. Sorry, nothing personal.
Maybe you can split the text in the testbox into array of strings (textBox1.Text.Split('\n')) and then use System.IO.File.WriteAllLines to write the array of strings to the file at once.
Share this answer

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