整理收藏:www.naio.net ,支援我們轉載保留此行|
如何讀取文字檔內容:
在本文介紹的程式中,是把讀取的文字檔,用一個richTextBox組件顯示出來。要讀取文字檔,必須使用到"StreamReader"類,這個類是由名字空間"System.IO"中定義的。通過"StreamReader"類的"ReadLine ( )"方法,就可以讀取開啟資料流當前行的資料了。下面代碼實現的功能就是讀取"C:\file.txt"並在richTextBox1組件中顯示出來:
FileStream fs = new FileStream ( "C:\\file.txt" , FileMode.Open , FileAccess.Read ) ;
StreamReader m_streamReader = new StreamReader ( fs ) ;
//使用StreamReader類來讀取檔案
m_streamReader.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;
// 從資料流中讀取每一行,直到檔案的最後一行,並在richTextBox1中顯示出內容
this.richTextBox1.Text = "" ;
string strLine = m_streamReader.ReadLine ( ) ;
while ( strLine != null )
{
this.richTextBox1.Text += strLine + "\n" ;
strLine = m_streamReader.ReadLine ( ) ;
}
//關閉此StreamReader對象
m_streamReader.Close ( ) ;
如何改變文字檔中資料內容:
在本文介紹的程式中,改變文字檔資料內容的功能是通過改變richTextBox1中的內容來實現的,當richTextBox1這的內容改變後,按動"另存新檔",就把richTextBox1中內容儲存到指定的文字檔中了。要想改變文字檔內容,要使用到"StreamWriter"類,這個類和"StreamReader"一樣,都是由"System.IO"名字空間來定義的。通過"StreamWriter"類的"Write ( )"方法,就可以輕鬆實現文字檔內容的更改了。下面代碼的功能是:如果"C"盤存在"file.txt",則把richTextBox1中的內容寫入到"file.txt"中,如果不存在,則建立此檔案,然後在寫入文本資料。
//建立一個檔案流,用以寫入或者建立一個StreamWriter
FileStream fs = new FileStream ( "C\\file.txt" , FileMode.OpenOrCreate , FileAccess.Write ) ;
StreamWriter m_streamWriter = new StreamWriter ( fs ) ;
m_streamWriter.Flush ( ) ;
// 使用StreamWriter來往檔案中寫入內容
m_streamWriter.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;
// 把richTextBox1中的內容寫入檔案
m_streamWriter.Write ( richTextBox1.Text ) ;
//關閉此檔案
m_streamWriter.Flush ( ) ;
m_streamWriter.Close ( ) ;
如何?預覽列印:
預覽列印是通過預覽列印對話方塊來實現的,實現對讀取得文字檔的預覽列印,最為重要的就是要通知預覽列印對話方塊所要預覽的檔案的內容。下面代碼就是把richTextBox1中顯示的內容,通過預覽列印對話方塊顯示出來:
//www.naio.net
string strText = richTextBox1.Text ;
StringReader myReader = new StringReader ( strText ) ;
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ( ) ;
printPreviewDialog1.Document = ThePrintDocument ;
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D ;
printPreviewDialog1.ShowDialog ( ) ;
下列代碼是設定列印內容即列印richTextBox1中的內容:
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 ) ;
//計算每一頁列印多少行
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight ( ev.Graphics ) ;
//重複使用StringReader對象 ,列印出richTextBox1中的所有內容
while ( count < linesPerPage && ( ( line = myReader.ReadLine ( ) ) != null ) )
{
// 計算出要列印的下一行所基於頁面的位置
yPosition = topMargin + ( count * printFont.GetHeight ( ev.Graphics ) ) ;
// 列印出richTextBox1中的下一行內容
ev.Graphics.DrawString ( line , printFont , myBrush , leftMargin , yPosition , new StringFormat ( ) ) ;
count++ ;
}
// 判斷如果還要下一頁,則繼續列印
if ( line != null )
ev.HasMorePages = true ;
else
ev.HasMorePages = false ;
myBrush.Dispose ( ) ;
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 ( )
{
//初始化表單中的各個組件
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 = "開啟" ;
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 = "預覽" ;
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 = "印表機設定" ;
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 = "開啟文字檔!" ;
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#來操作文字檔" ;
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類來讀取檔案
m_streamReader.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;
// 從資料流中讀取每一行,直到檔案的最後一行,並在richTextBox1中顯示出內容
this.richTextBox1.Text = "" ;
string strLine = m_streamReader.ReadLine ( ) ;
while ( strLine != null )
{
this.richTextBox1.Text += strLine + "\n" ;
strLine = m_streamReader.ReadLine ( ) ;
}
//關閉此StreamReader對象
m_streamReader.Close ( ) ;
}
}
catch ( Exception em )
{
Console.WriteLine ( em.Message.ToString ( ) ) ;
}
}
private void button3_Click ( object sender , System.EventArgs e )
{
try
{
//獲得另存新檔的檔案名稱
if ( saveFileDialog1.ShowDialog ( ) == DialogResult.OK )
{
//建立一個檔案流,用以寫入或者建立一個StreamWriter
FileStream fs = new FileStream ( @saveFileDialog1.FileName , FileMode.OpenOrCreate , FileAccess.Write ) ;
StreamWriter m_streamWriter = new StreamWriter ( fs ) ;
m_streamWriter.Flush ( ) ;
// 使用StreamWriter來往檔案中寫入內容
m_streamWriter.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;
// 把richTextBox1中的內容寫入檔案
m_streamWriter.Write ( richTextBox1.Text ) ;
//關閉此檔案
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 ( ) ;
}
//預覽列印文檔
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中的內容
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 ) ;
//計算每一頁列印多少行
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight ( ev.Graphics ) ;
//重複使用StringReader對象 ,列印出richTextBox1中的所有內容
while ( count < linesPerPage && ( ( line = myReader.ReadLine ( ) ) != null ) )
{
// 計算出要列印的下一行所基於頁面的位置
yPosition = topMargin + ( count * printFont.GetHeight ( ev.Graphics ) ) ;
// 列印出richTextBox1中的下一行內容
ev.Graphics.DrawString ( line , printFont , myBrush , leftMargin , yPosition , new StringFormat ( ) ) ;
count++ ;
}
// 判斷如果還要下一頁,則繼續列印
if ( line != null )
ev.HasMorePages = true ;
else
ev.HasMorePages = false ;
myBrush.Dispose ( ) ;
}
}
轉載自:http://www.weizone.com/viewthread.php?tid=157