Preface
When you are using C# development, you may need to write some information to txt. Here are some commonly used methods.
method:
1. Write an array composed of strings to txt
This method does not require Flush and Close().
If not, the system will automatically help create one
string[] lines = { "This is the first line", "This is the second line", "This is the third line" }; (@"When you want to save", lines);
2. Write a string to txt
This method does not require Flush and Close().
If not, the system will automatically help create one
If you want to break the line, add "\r\n" to the break line
string text = "This is the first line" + "\r\n" +"This is the second line"; (@"When you want to save", text);
3. Use StreamWriter
This method Using replaces the functions of Flush and Close()
If not, the system will automatically help create one
Automatic line wrap
If you want to add content to the original txt file, set the method in StreamWriter to true
If the original file is overwritten, set to false
string[] lines = { "This is the first line", "This is the second line", "This is the third line" }; using ( file = new (@"When you want to save",false)) { foreach (string line in lines) { (line); } }
Summarize
This is the end of this article about several common methods of writing txt data in C#. For more related contents of writing txt data in C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!