This article describes the method of C# processing text file TXT. Share it for your reference. The specific analysis is as follows:
1. How to read the content of the text file:
The program introduced here is to display the read text file using a richTextBox component. To read a text file, you must use the "StreamReader" class, which is defined in the namespace "". Through the "ReadLine()" method of the "StreamReader" class, you can read the data that opens the current line of the data stream. The function implemented by the following code is to read "C:\" and display it in the richTextBox1 component:
FileStreamfs=newFileStream("C:\\",,); StreamReaderm_streamReader=newStreamReader(fs); //Use StreamReader class to read filesm_streamReader.(0,); //Read each line from the data stream until the last line of the file and display the content in richTextBox1this.=""; stringstrLine=m_streamReader.ReadLine(); while(strLine!=null) { this.+=strLine+"\n"; strLine=m_streamReader.ReadLine(); } //Close this StreamReader objectm_streamReader.Close();
2. How to change the data content in a text file:
In the following program, the function of changing the data content of the text file is achieved by changing the content in richTextBox1. When the content of richTextBox1 is changed, press "Save As" to store the content in richTextBox1 into the specified text file. To change the content of the text file, you need to use the "StreamWriter" class. Like "StreamReader", this class is defined by the "" namespace. Through the "Write()" method of the "StreamWriter" class, you can easily achieve changes in text file content. The function of the following code is: if the "C" disk exists", write the contents in richTextBox1 into "", if it does not exist, create this file, and then write text data.
//Create a file stream to write or create a StreamWriterFileStreamfs=newFileStream("C\\",,); StreamWriterm_streamWriter=newStreamWriter(fs); m_streamWriter.Flush(); //Use StreamWriter to write content in the filem_streamWriter.(0,); //Write the contents in richTextBox1 to the filem_streamWriter.Write(); //Close this filem_streamWriter.Flush(); m_streamWriter.Close();
From the above two codes, it is easier to write data than to read data.
3. How to implement print preview:
Print preview is implemented through the print preview dialog box, which realizes print preview of the text file. The most important thing is to notify the content of the file to be previewed in the print preview dialog box. The following code shows the content displayed in richTextBox1 through the print preview dialog box:
stringstrText=; StringReadermyReader=newStringReader(strText); PrintPreviewDialogprintPreviewDialog1=newPrintPreviewDialog(); =ThePrintDocument; =FormBorderStyle.Fixed3D; ();
4. How to print a file:
A class "PrintDocument" is defined in the namespace "". By calling this "Print" method, another event "PrintPage" encapsulated in this namespace can be triggered. In this event, set the document content to be printed, thereby realizing the printing of the queue text file. The following code is to call the "Print" method of "PrintDocument" and call the event "PrintPage" to print the content in richTextBox1:
();// Where ThePrintDocument is an object of the "PrintDocument" class
The following code is to set the print content, that is, print the content in richTextBox1:
floatlinesPerPage=0; floatyPosition=0; intcount=0; floatleftMargin=; floattopMargin=; stringline=null; FontprintFont=; SolidBrushmyBrush=newSolidBrush(); // Calculate how many lines are printed on each pagelinesPerPage=/(); //Reuse the StringReader object to print out all the contents in richTextBox1while(count<linesPerPage&&((line=())!=null)) { // Calculate the location of the page on which the next line to be printedyPosition=topMargin+(count*()); //Print out the next line in richTextBox1(line,printFont,myBrush,leftMargin,yPosition,newStringFormat()); count++; } //Judge if you want the next page, continue printingif(line!=null) =true; else =false; ();
Notes:Since the name space for these classes is omitted from the above code, in order to successfully compile and run the above code, the name space used must be imported at the program header.
I hope this article will be helpful to everyone's C# programming.