本篇內(nèi)容主要講解“C#處理文本文件的方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C#處理文本文件的方法”吧!
成都創(chuàng)新互聯(lián)公司成立與2013年,先為新津縣等服務(wù)建站,新津縣等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為新津縣企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
用C#處理文本文件的完整源程序代碼(control.cs),現(xiàn)在就可以方便的得到用C#處理文本文件的一個(gè)完整的源程序,具體如下:
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.IO ;
using System.Drawing.Printing ;
public class Form1 : Form
{
private RichTextBox richTextBox1 ;
private Button button1 ;
private Button button2 ;
private Button button3 ;
private Button button4 ;
private Button button5 ;
private OpenFileDialog openFileDialog1 ;
private SaveFileDialog saveFileDialog1 ;
private PrintDialog printDialog1 ;
private PrintDocument ThePrintDocument ;
private PrintPreviewDialog printPreviewDialog1 ;
private StringReader myReader ;
private System.ComponentModel.Container components = null ;
public Form1 ( )
{
//初始化窗體中的各個(gè)組件
InitializeComponent ( ) ;
}
//清除程序中使用多的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
private void InitializeComponent ( )
{
richTextBox1 = new RichTextBox ( ) ;
button1 = new Button ( ) ;
button2 = new Button ( ) ;
button3 = new Button ( ) ;
button4 = new Button ( ) ;
button5 = new Button ( ) ;
saveFileDialog1 = new SaveFileDialog ( ) ;
openFileDialog1 = new OpenFileDialog ( ) ;
printPreviewDialog1 = new PrintPreviewDialog ( ) ;
printDialog1 = new PrintDialog ( ) ;
ThePrintDocument = new PrintDocument ( ) ;
ThePrintDocument.PrintPage += new PrintPageEventHandler
( ThePrintDocument_PrintPage ) ;SuspendLayout ( ) ;
richTextBox1.Anchor = AnchorStyles.None ;
richTextBox1.Name = "richTextBox1" ;
richTextBox1.Size = new Size ( 448 , 280 ) ;
richTextBox1.TabIndex = 0 ;
richTextBox1.Text = "" ;
button1.Anchor = AnchorStyles.None ;
button1.Location = new Point ( 41 , 289 ) ;
button1.Name = "button1" ;
button1.Size = new Size ( 48 , 30 ) ;
button1.TabIndex = 1 ;
button1.Text = "打開(kāi)" ;
button1.Click += new System.EventHandler ( button1_Click ) ;
button2.Anchor = AnchorStyles.None ;
button2.Location = new Point ( 274 , 288 ) ;
button2.Name = "button2" ;
button2.Size = new Size ( 48 , 30 ) ;
button2.TabIndex = 4 ;
button2.Text = "預(yù)覽" ;
button2.Click += new System.EventHandler ( button2_Click ) ;
button3.Anchor = AnchorStyles.None ;
button3.Location = new Point ( 108 , 288 ) ;
button3.Name = "button3" ;
button3.Size = new Size ( 48 , 30 ) ;
button3.TabIndex = 2 ;
button3.Text = "保存" ;
button3.Click += new System.EventHandler ( button3_Click ) ;
button4.Anchor = AnchorStyles.None ;
button4.Location = new Point ( 174 , 288 ) ;
button4.Name = "button4" ;
button4.Size = new Size ( 80 , 30 ) ;
button4.TabIndex = 3 ;
button4.Text = "打印機(jī)設(shè)置" ;
button4.Click += new System.EventHandler ( button4_Click ) ;
button5.Anchor = AnchorStyles.None ;
button5.Location = new Point ( 345 , 288 ) ;
button5.Name = "button5" ;
button5.Size = new Size ( 48 , 30 ) ;
button5.TabIndex = 5 ;
button5.Text = "打印" ;
button5.Click += new System.EventHandler ( button5_Click ) ;
saveFileDialog1.DefaultExt = "*.txt" ;
saveFileDialog1.FileName = "file.txt" ;
saveFileDialog1.InitialDirectory = "c:\\" ;
saveFileDialog1.Title = "另存為!" ;
openFileDialog1.DefaultExt = "*.txt" ;
openFileDialog1.FileName = "file.txt" ;
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Title = "打開(kāi)文本文件!" ;
AutoScaleBaseSize = new Size ( 6 , 14 ) ;
ClientSize = new Size ( 448 , 325 ) ;
this.Controls.Add ( button1 ) ;
this.Controls.Add ( button2 ) ;
this.Controls.Add ( button3 ) ;
this.Controls.Add ( button4 ) ;
this.Controls.Add ( button5 ) ;
this.Controls.Add ( richTextBox1 ) ;
this.MaximizeBox = false ;
this.Name = "Form1" ;
this.Text = "C#來(lái)操作文本文件" ;
this.ResumeLayout ( false ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void button1_Click ( object sender , System.EventArgs e )
{
try
{
if ( openFileDialog1.ShowDialog ( ) == DialogResult.OK )
{
FileStream fs = new FileStream ( openFileDialog1.FileName,
FileMode.Open , FileAccess.Read ) ;StreamReader m_streamReader = new StreamReader ( fs ) ;
//使用StreamReader類(lèi)來(lái)讀取文件
m_streamReader.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;
// 從數(shù)據(jù)流中讀取每一行,直到文件的***一行,并在richTextBox1中顯示出內(nèi)容
this.richTextBox1.Text = "" ;
string strLine = m_streamReader.ReadLine ( ) ;
while ( strLine != null )
{
this.richTextBox1.Text += strLine + "\n" ;
strLine = m_streamReader.ReadLine ( ) ;
}
//關(guān)閉此StreamReader對(duì)象
m_streamReader.Close ( ) ;
}
}
catch ( Exception em )
{
Console.WriteLine ( em.Message.ToString ( ) ) ;
}
}
private void button3_Click ( object sender , System.EventArgs e )
{
try
{
//獲得另存為的文件名稱(chēng)
if ( saveFileDialog1.ShowDialog ( ) == DialogResult.OK )
{
//創(chuàng)建一個(gè)文件流,用以寫(xiě)入或者創(chuàng)建一個(gè)StreamWriter
FileStream fs = new FileStream ( @saveFileDialog1.FileName,
FileMode.OpenOrCreate , FileAccess.Write ) ;StreamWriter m_streamWriter = new StreamWriter ( fs ) ;
m_streamWriter.Flush ( ) ;
// 使用StreamWriter來(lái)往文件中寫(xiě)入內(nèi)容
m_streamWriter.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;
// 把richTextBox1中的內(nèi)容寫(xiě)入文件
m_streamWriter.Write ( richTextBox1.Text ) ;
//關(guān)閉此文件
m_streamWriter.Flush ( ) ;
m_streamWriter.Close ( ) ;
}
}
catch ( Exception em )
{
Console.WriteLine ( em.Message.ToString ( ) ) ;
}
}
private void button4_Click ( object sender , System.EventArgs e )
{
printDialog1.Document = ThePrintDocument ;
printDialog1.ShowDialog ( ) ;
}
//預(yù)覽打印文檔
private void button2_Click ( object sender , System.EventArgs e )
{
try
{
string strText = richTextBox1.Text ;
myReader = new StringReader ( strText ) ;
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ( ) ;
printPreviewDialog1.Document = ThePrintDocument;
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
printPreviewDialog1.ShowDialog ( ) ;
}
catch ( Exception exp )
{
Console.WriteLine ( exp.Message.ToString ( ) ) ;
}
}
//打印richTextBox1中的內(nèi)容
private void button5_Click ( object sender , System.EventArgs e )
{
printDialog1.Document = ThePrintDocument ;
string strText = richTextBox1.Text ;
myReader = new StringReader ( strText ) ;
if ( printDialog1.ShowDialog ( ) == DialogResult.OK )
{
ThePrintDocument.Print ( ) ;
}
}
protected void ThePrintDocument_PrintPage ( object sender , PrintPageEventArgs ev )
{
float linesPerPage = 0 ;
float yPosition = 0 ;
int count = 0 ;
float leftMargin = ev.MarginBounds.Left ;
float topMargin = ev.MarginBounds.Top ;
string line = null ;
Font printFont = richTextBox1.Font ;
SolidBrush myBrush = new SolidBrush ( Color.Black ) ;
//計(jì)算每一頁(yè)打印多少行
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight ( ev.Graphics ) ;
//重復(fù)使用StringReader對(duì)象 ,打印出richTextBox1中的所有內(nèi)容
while ( count < linesPerPage && ( ( line = myReader.ReadLine ( ) ) != null ) )
{
// 計(jì)算出要打印的下一行所基于頁(yè)面的位置
yPosition = topMargin + ( count * printFont.GetHeight ( ev.Graphics ) ) ;
// 打印出richTextBox1中的下一行內(nèi)容
ev.Graphics.DrawString ( line , printFont , myBrush ,
leftMargin , yPosition , new StringFormat ( ) ) ;count++ ;
}
// 判斷如果還要下一頁(yè),則繼續(xù)打印
if ( line != null )
ev.HasMorePages = true ;
else
ev.HasMorePages = false ;
myBrush.Dispose ( ) ;
}
}
本文雖然只是介紹了用C#處理文本文件,但其實(shí)對(duì)C#處理其他文件也有很多的參考價(jià)值,這是因?yàn)樵诿挚臻g"System.IO"中定義的用以處理其他文件的類(lèi)的結(jié)構(gòu)和用以處理文本文件的類(lèi)的結(jié)構(gòu)有很多是相同的。希望本文對(duì)你用C#進(jìn)行文件方面的編程有所幫助。
到此,相信大家對(duì)“C#處理文本文件的方法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!