SoFunction
Updated on 2025-03-07

C# simple way to read and write txt files

This article describes the method of simply reading and writing txt files in C#. Share it for your reference, as follows:

//write txt
StringBuilder builder = new StringBuilder();
FileStream fs = new FileStream(saveFileName, );
StreamWriter sw = new StreamWriter(fs, );
for (int i = 0; i < [0].; i++)
{
  DataRow dr = [0].Rows[i];
  (dr["netsn"] + "," + dr["imei"]); //Product S/N + IMEI}
(builder);
();
();
if ((saveFileName))
{
  (saveFileName); //Open this file after saving successfully}
//read txt
string[] allLines = (filePath);

For more information about C# related content, please check out the topic of this site:Summary of common techniques for C# file operation》、《Summary of thread usage techniques for C# programming》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.