C # Processing text file txt instance in detail

Source: Internet
Author: User
Tags processing text

An Jingping and fonts: [Increase decrease] Type: Reprint time: 2015-02-02 I want to comment

This article mainly describes the C # processing text file txt method, in case of a detailed analysis of the TXT text file reading, modification and printing functions such as the implementation of skills, the need for friends can refer to the next

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 ();

Note: Since these classes are omitted from the above code in the namespace, so in order to successfully compile and run the above code, it is necessary to import the name space used in the program header.

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

C # Processing text file txt instance in detail (go)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.