C# Read and write to text file Newline, with one line as multiple lines
Write the text to the file. If several paragraphs of text are put together and output to the file, usually a newline needs to be added after each line, otherwise several paragraphs of text will become one paragraph. If all the content in the text file is one line, read them to the text box, they will be one line, and vice versa; if they are required to be divided into multiple lines, each line displays the specified number of characters, and a newline needs to be added after each line.
There are two methods for text wrapping in C#. One is to add a newline \r\n after the text that needs to be wrapped. The other method is to use Environment.NewLine, which has the same function as \r\n. Specific implementation code for two methods is as follow.
I, C# write to text file newline
1. C# write to text file newLine with \r\n
A \r\n means a new line; if you want to change two lines, repeat \r\n, that is, \r\n\r\n; if you change three lines, repeat three times, that is, \r\n\r\n\r\n; and so on. The code example is as follows:
using System.IO;
/// <summary>
/// C# write to txt file newLine with \r\n
/// </summary>
/// <param name="filePath">Path of text file</param>
public void SaveTextFileNewline(string filePath)
{
string text = "C# write to txt file" + :"\r\n" + "Other text";// \r\n means a new line
try
{
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
{
StreamWriter write = new StreamWriter(fs);
write.Write(text);
write.Flush();
write.Close();
fs.Close();
}
}
catch
{
// Error saving text file!
}
}
Call: SaveTextFileNewline( @"F:\temp\txtName.txt");
2. C# write to text file newLine with Environment.NewLine
The following code is also to write text to a text file, the implementation method is the same as above, but use Environment.NewLine instead of \r\n.
using System.IO;
/// <summary>
/// C# write to text file newLine with Environment.NewLine
/// </summary>
/// <param name="filePath">Path of text file</param>
public void SaveTextFileNewline(string filePath)
{
try
{
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
{
StreamWriter write = new StreamWriter(fs);
write.BaseStream.Seek(0, SeekOrigin.End);
write.WriteLine("Write to text file newLine with Environment.NewLine;");
write.WriteLine(Environment.NewLine);
write.WriteLine("Wrap paragraphs.");
write.Flush();
write.Close();
fs.Close();
}
}
catch
{
// Error saving text file!
}
}
Call: SaveTextFileNewline( @"F:\temp\txtName.txt");
3. The content of Textbox is not wrapped after being saved to a txt file
If the content of the Textbox is saved to a text file without line breaks, it is because the newline of the Textbox is \n and the newline of the text file is \r\ n. Only by replacing \n with \r\n, the code is as follows:
using System.IO;
private void SaveTextToTextFile(string filePath)
{
string text = textbox1.Text;
if (string.IsNullOrEmpty(text))
return;
text = text.Replace("\n", "\r\n");
File.WriteAllText(filePath, text);
}
Call: SaveTextToTextFile(@"F:\temp\txtName.txt");
II, C# read from text file newline
1. C# displays one line as multiple lines
If the content in the text file is one line, it needs to be read out and displayed as multiple lines, displays 20 characters each line, the code is as follows:
using System.IO;
private void ReadTextFromTextFile(string filePath)
{
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
StreamReader srd = new StreamReader(fs);
string line = srd.ReadLine(), text = line;
while (line != null)
{
line = srd.ReadLine();
text += line;
}
srd.Close();
fs.Close();
textbox1.Text = RichTextBoxDisplayNLines(text, 1000, 20);
}
}
catch
{
// Error reading text file!
}
}
Description: The RichTextBoxDisplayNLines method is described in the article "C# Richtextbox change font color, add and foreach line, select text, line, display the first n lines".
Call: ReadTextFromTextFile(@"F:\temp\txtName.txt");
2. C# Newlin after semicolon
To read content from a text file, if you need to wrap a line after each semicolon, you need to add a newline after the semicolon as \n.
private void ReadTextFromTxtFileNewlineFromSemicolon(string filePath)
{
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
StreamReader srd = new StreamReader(fs);
string text = srd.ReadToEnd();
srd.Close();
fs.Close();
if (!string.IsNullOrEmpty(text))
textbox1.Text = text.Replace(";", ";\r\n");
}
}
catch
{
//Error reading text file!
}
}
Call: ReadTextFromTxtFileNewlineFromSemicolon(@"F:\temp\txtName.txt");
-
Related Reading
- C# label control, with winforms label transparent ba
- C# Richtextbox change font color, add and foreach li
- C# sort files or folders by name, date and creation
- C# Windows Forms foreach controls, with Textbox and
- C# sorted dictionary by value, key or length of the
- C# Winforms panel border, with color, style, thickne
- C# random number generator in range, with double,str