This example describes how C # handles txt in a text file. Share to everyone for your reference. The specific analysis is as follows:
1. How to read the contents of a text file:
The program described here is to read the text file, with a RichTextBox component display. To read a text file, you must use the "StreamReader" class, which is defined by the namespace "System.IO". With the "ReadLine ()" Method of the "StreamReader" class, you can read the data that opens the current row of the data flow. The following code implements the function of reading "C:\file.txt" and displaying it in the RichTextBox1 component:
Filestreamfs=newfilestream ("C:\\file.txt", FileMode.Open,FileAccess.Read); Streamreaderm_streamreader=newstreamreader (fs);//Use the StreamReader class to read the file M_streamReader.BaseStream.Seek (0, Seekorigin.begin);//reads each row from the data stream until the last line of the file, and displays the content this.richtextbox1.text= "" in RichTextBox1; stringstrline=m_ StreamReader.ReadLine (); while (strline!=null) {this.richtextbox1.text+=strline+ "\ n"; strline=m_ StreamReader.ReadLine ();} Close this StreamReader object M_streamreader.close ();
2. How to change the content of data in a text file:
In the following program, the function of changing the content of the text file data is implemented by changing the contents of the RichTextBox1, and when the content of RichTextBox1 is changed, the "Save as" is pressed to save the contents of the RichTextBox1 in the specified text file. To change the contents of a text file, use the "StreamWriter" class, which, like "StreamReader", is defined by the "System.IO" namespace. The "Write ()" Method of the "StreamWriter" class makes it easy to make changes to the contents of the text file. The function of the following code is that if the "C" disk exists "file.txt", the contents of the RichTextBox1 are written to "file.txt", if not present, the file is created, and then the text data is written.
Create a file stream to write or create a streamwriterfilestreamfs=newfilestream ("C\\file.txt", FileMode.OpenOrCreate, FileAccess.Write); Streamwriterm_streamwriter=newstreamwriter (fs); M_streamwriter.flush ();//write content using StreamWriter to and from files M_ StreamWriter.BaseStream.Seek (0,seekorigin.begin);//write the contents of the RichTextBox1 to the file M_streamwriter.write (richtextbox1.text );//close this file M_streamwriter.flush (); M_streamwriter.close ();
From the above two code, it is easier to write the data than to read the data.
3. How to achieve Print preview:
The print preview is implemented through the Print Preview dialog box, which implements a print preview of the read-to-text file and, most importantly, notifies the content of the file to be previewed by the Print Preview dialog box. The following code shows the contents shown in the RichTextBox1 in the Print Preview dialog box:
Stringstrtext=richtextbox1.text; Stringreadermyreader=newstringreader (StrText); Printpreviewdialogprintpreviewdialog1=newprintpreviewdialog ();p rintpreviewdialog1.document=theprintdocument; Printpreviewdialog1.formborderstyle=formborderstyle.fixed3d;printpreviewdialog1.showdialog ();
4. How to print a file:
A class "PrintDocument" is defined in the namespace "System.Drawing.Printing", and another event "PrintPage" encapsulated in this namespace can be triggered by invoking the "Print" method of this class. In this event, you set the content of the document to be printed, which allows you to print the team text file. The following code calls the "print" Method of "PrintDocument", and invokes the event "PrintPage" to print the contents of the RichTextBox1:
Theprintdocument.print ();//Where theprintdocument is an object of the "PrintDocument" class
The following code sets the contents of the print RichTextBox1:
Floatlinesperpage=0;floatyposition=0;intcount=0;floatleftmargin=ev. Marginbounds.left;floattopmargin=ev. Marginbounds.top;stringline=null; Fontprintfont=richtextbox1.font; Solidbrushmybrush=newsolidbrush (Color.Black);//calculates how many lines of Linesperpage=ev are printed on each page. Marginbounds.height/printfont.getheight (EV. Graphics),//re-use the StringReader object, print out all the contents of the RichTextBox1 while (count<linesperpage&& (line= Myreader.readline ())!=null) {//calculates the position of the page based on the next line to be printed yposition=topmargin+ (Count*printfont.getheight (EV). Graphics);//print out the next line of content in RichTextBox1 ev.Graphics.DrawString (Line,printfont,mybrush,leftmargin,yposition, Newstringformat ()); count++;} Determine if you want the next page to continue printing if (line!=null) EV. Hasmorepages=true;elseev. Hasmorepages=false;mybrush.dispose ();
Comments:
In order to compile and run the above code successfully, it is necessary to import the namespace used in the program header because the above code is omitted from the namespace.
I hope this article is helpful to everyone's C # programming.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Processing text file txt instance in detail
This address: http://www.paobuke.com/develop/c-develop/pbk23148.html
Related content How to use application in ASP. C # WinForm Control drag and zoom implementation Code C # How to get the hard drive serial number problem summary C # How to implement Picture viewer
About C # Regular expressions regex common match super cool WPF implementation loading control effect C # use Matrix to perform scaling method C # Queue Usage instance analysis
C # Processing text file txt instance in detail