C processing text file TXT instance detail

  • 2020-12-26 05:50:46
  • OfStack

This article gives an example of how C# works with text files TXT. Share to everybody for everybody reference. The specific analysis is as follows:

1. How to read text file contents:

In the program described here, the text file is read and displayed with an richTextBox component. To read a text file, you must use the "StreamReader" class, which is defined in the namespace "System.IO". With the "ReadLine()" method of the "StreamReader" class, you can read the current row of the open data stream. The following code reads "C:\ file.txt "and displays it in the richTextBox1 component:


FileStreamfs=newFileStream("C:\\file.txt",FileMode.Open,FileAccess.Read);
StreamReaderm_streamReader=newStreamReader(fs);
// use StreamReader Class to read the file 
m_streamReader.BaseStream.Seek(0,SeekOrigin.Begin);
// Read each from the data stream 1 Line up to the end of the file 1 Line, and in the richTextBox1 Displays the contents 
this.richTextBox1.Text="";
stringstrLine=m_streamReader.ReadLine();
while(strLine!=null)
{
this.richTextBox1.Text+=strLine+"\n";
strLine=m_streamReader.ReadLine();
}
// Shut down the StreamReader object 
m_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 of richTextBox1. When the content of richTextBox1 is changed, press "save as" to store the content of richTextBox1 in the specified text file. To change the text file content, use the "StreamWriter" class, which, like "StreamReader"1, is defined by the "System.IO" namespace. The "Write()" method of the "StreamWriter" class makes it easy to change the content of a text file. If the "C" disk exists on "file.txt", then write the contents of richTextBox1 to "file.txt". If not, then create this file and then write the text data.


// create 1 Three file streams for writing or creating 1 a StreamWriter
FileStreamfs=newFileStream("C\\file.txt",FileMode.OpenOrCreate,FileAccess.Write);
StreamWriterm_streamWriter=newStreamWriter(fs);
m_streamWriter.Flush();
// use StreamWriter Write to and from a file 
m_streamWriter.BaseStream.Seek(0,SeekOrigin.Begin);
// the richTextBox1 Writes to the file 
m_streamWriter.Write(richTextBox1.Text);
// Close this file 
m_streamWriter.Flush();
m_streamWriter.Close();

From these two pieces of code, it is easier to write data than to read it.

3. How to achieve print preview:

Print preview is achieved through the print preview dialog box, to read and get the text file print preview, the most important thing is to inform the print preview dialog box to preview the content of the file. The following code displays the contents shown in richTextBox1 through the print preview dialog box:


stringstrText=richTextBox1.Text;
StringReadermyReader=newStringReader(strText);
PrintPreviewDialogprintPreviewDialog1=newPrintPreviewDialog();
printPreviewDialog1.Document=ThePrintDocument;
printPreviewDialog1.FormBorderStyle=FormBorderStyle.Fixed3D;
printPreviewDialog1.ShowDialog();

4. How to print a document:

In the namespace "System.Drawing.Printing", a class "PrintDocument" is defined, and by calling the "Print" method of this class, another event "PrintPage" encapsulated in the namespace can be triggered. In this event, set the content of the document to be printed to print a text file. The following code calls the "Print" method of "PrintDocument" and calls the event "PrintPage" to print the contents of richTextBox1:


ThePrintDocument.Print();// Among them ThePrintDocument is "PrintDocument" Of the class 1 An object 

The following code is to set the print content, that is, to print the content in richTextBox1:


floatlinesPerPage=0;
floatyPosition=0;
intcount=0;
floatleftMargin=ev.MarginBounds.Left;
floattopMargin=ev.MarginBounds.Top;
stringline=null;
FontprintFont=richTextBox1.Font;
SolidBrushmyBrush=newSolidBrush(Color.Black);
// Calculate each 1 How many lines does the page print 
linesPerPage=ev.MarginBounds.Height/printFont.GetHeight(ev.Graphics);
// Repeated use StringReader object , Print out the richTextBox1 All of the content in 
while(count<linesPerPage&&((line=myReader.ReadLine())!=null))
{
// Figure out what to print 1 Rows are based on the location of the page 
yPosition=topMargin+(count*printFont.GetHeight(ev.Graphics));
// Print out the richTextBox1 Under the 1 line 
ev.Graphics.DrawString(line,printFont,myBrush,leftMargin,yPosition,newStringFormat());
count++;
}
// Judge if I have to do it again 1 Page, continue to print 
if(line!=null)
ev.HasMorePages=true;
else
ev.HasMorePages=false;
myBrush.Dispose();

Note: To successfully compile and run the above code, you need to import the namespace used in the header of the program because you have omitted the namespace to which these classes are attached.

I hope this article has been helpful for your C# programming.


Related articles: