Text files are a common file format, so how to process text files becomes a focus of programming. This article will discuss how C # is used to process text files. The content focuses on how to read the content of a text file, how to change the content of a text file, and how to use C # To print, preview, and print the read text file.
I. software environment for program design and operation in this article:
(1). Microsoft Windows 2000 Server Edition
(2). Net FrameWork SDK Beta 2
II. C # important processes for processing text files:
(1). How to read text file content:
In the program described in this article, the read text file is displayed using a richTextBox component. To read a text file, you must use the "StreamReader" class, which is defined in the namespace "System. IO. You can use the "ReadLine ()" method of the "StreamReader" class to read the data from the current row of the data stream. The following code reads "C: file.txt" and displays it in the richTextBox1 component:
FileStream fs = new FileStream ("C: \ file.txt", FileMode. Open, FileAccess. Read );
StreamReader m_streamReader = new StreamReader (fs );
// Use the StreamReader class to read files
M_streamReader.BaseStream.Seek (0, SeekOrigin. Begin );
// Read each row from the data stream until the last row of the file is displayed in richTextBox1.
This. richTextBox1.Text = "";
String strLine = m_streamReader.ReadLine ();
While (strLine! = Null)
{
This. richTextBox1.Text + = strLine + "";
StrLine = m_streamReader.ReadLine ();
}
// Close this StreamReader object
M_streamReader.Close ();
(2). How to change the data content in a text file:
In the program described in this article, the function of changing the data content of a text file is implemented by changing the content in richTextBox1. When the content in richTextBox1 changes, press "Save ", the content in richTextBox1 is stored in the specified text file. To change the content of a text file, you must use the "StreamWriter" class. This class, like "StreamReader", is defined by the "System. IO" namespace. With the "Write ()" method of the "StreamWriter" class, you can easily change the content of a text file. The function of the following code is: if "C" disk has "file.txt", write the content in richTextBox1 to "file.txt". If it does not exist, create this file, then, write text data.
// Create a file stream for writing or creating a StreamWriter
FileStream fs = new FileStream ("C \ file.txt", FileMode. OpenOrCreate, FileAccess. Write );
StreamWriter m_streamWriter = new StreamWriter (fs );
M_streamWriter.Flush ();
// Use StreamWriter to write content to files
M_streamWriter.BaseStream.Seek (0, SeekOrigin. Begin );
// Write the content in richTextBox1 to the file
M_streamWriter.Write (richTextBox1.Text );
// Close this file
M_streamWriter.Flush ();
M_streamWriter.Close ();
From the above two codes, writing data is easier than reading data.
(3) how to print and preview:
Print preview is implemented through the print preview dialog box. To print and preview a text file after reading, the most important thing is to notify the print preview dialog box of the content of the file to be previewed. The following code displays the content displayed in richTextBox1 in the print preview dialog box:
String strText = richTextBox1.Text;
StringReader myReader = new StringReader (strText );
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ();
PrintPreviewDialog1.Document = ThePrintDocument;
PrintPreviewDialog1.FormBorderStyle = FormBorderStyle. Fixed3D;
PrintPreviewDialog1.ShowDialog ();
(4). How to print files:
In the namespace "System. drawing. printing "defines a class" PrintDocument ". By calling the" Print "method of this class, another event" PrintPage "encapsulated in this namespace can be triggered ". In this event, set the document content to be printed to print the team text file. The following code calls the "Print" method of "PrintDocument" and calls the event "PrintPage" to Print the content in richTextBox1:
ThePrintDocument. Print (); // Where ThePrintDocument is an object of the "PrintDocument" Class
Run the following code to print the printed content in richTextBox1:
Float linesPerPage = 0;
Float yPosition = 0;
Int count = 0;
Float leftMargin = ev. MarginBounds. Left;
Float topMargin = ev. MarginBounds. Top;
String line = null;