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 ( ) ;
//创建一个文件流,用以写入或者创建一个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 ( ) ;
从上面这二个代码可以,写入数据比起读取数据要显得容易些。
(3).如何实现打印预览:
string strText = richTextBox1.Text ;
StringReader myReader = new StringReader ( strText ) ;
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ( ) ;
printPreviewDialog1.Document = ThePrintDocument ;
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D ;
printPreviewDialog1.ShowDialog ( ) ;
(4).如何打印文件:
ThePrintDocument.Print ( ) ;//其中ThePrintDocument是"PrintDocument"类的一个对象
下列代码是设定打印内容即打印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 ( ) ;
三. 用C#处理文本文件的完整源程序代码(control.cs):
掌握了上面这些关键步骤,就可以方便的得到用C#来处理文本文件的一个完整的源程序,具体如下:
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 ( ) ;
}
}
分享到:
相关推荐
C#处理文本文件和打印和打印预览,非常有用
C#处理文本文件和实现MD5,有源码和使用说明
本文实例讲述了C#处理文本文件TXT的方法。分享给大家供大家参考。具体分析如下: 1. 如何读取文本文件内容: 这里介绍的程序中,是把读取的文本文件,用一个richTextBox组件显示出来。要读取文本文件,必须使用到”...
C#中的文本文件处理方法,要写集成电路的测试真值,所以想找一个好一点的办法。
C#356-处理文本文件源代码
C#处理word文件汇总。从网上搜集总结的各种word操作方法
C#处理字幕文件(srt和Lrc格式)并根据时间进行剪辑,可以从srt文件和lrc文件中解析出里面每一段的字幕形成集合。并且可以根据一个所给的开始时间和结束时间来或者一个处于两者之间字幕的集合。这个工具类只支持最长...
vs2010-c#读取txt文件至DataTable经过处理后导出txt 某油田项目中数据需要处理,里面包括项目中的几个txt数据文件,主要有参考意义的是里面读取txt方法和写入txt方法 简洁有效,处理方法是针对特定数据结构编写的。
利用C#处理文本文件,提取数据进行处理,得到日平均气温
用于无人机影像处理读取外方为元素到数组,实现文本文件读入,并制作成二维数组 ,分两种情况,以逗号分隔,和以不定数目空格进行分隔。
C#实现将WAVE文件的按字节读写,然后生成文本文件,最终还原成WAVE文件的实验过程。
内有全面的字符串方法的详细介绍和使用实例,因为一般教材中都不会写的那么详细,这是一个资深程序员自己的学习总结,清晰易懂,对于初学者来说真是...还有文本文件处理的常用方法及使用实例。 ——转自百度空间。
C# 文件处理技术,包括file,fileinfo等,具体如下 第三章 文件处理技术 2 3-1 System.IO 命名空间 2 3-1-1 System.IO类介绍 2 3-1-2 File类的常用方法 4 3-1-3 Fileinfo类的常用方法 5 3-1 Fileinfo类的常用方法 5 ...
C#实现将JPEG文件的按字节读写,然后生成文本文件,最终还原成JPEG文件的实验进程。
基于C#下的,对txt文件进行读写,可以按分隔符读取,保存
C#ie实现自动下载示例、C#Socket基本编程、C# WinForm制作异形窗体与控件、C# 编写定时关机程序、C#对注册表的操作、C# 简单文本文件读写、C# 控制远程计算机的服务的方法、C# 木马寄存方式收集、C#实现通过程序自动...
支持处理编码格式"GB2312", "GBK", "HZ"等
基于C#写的文件拆分和合并处理程序,源码和应用都有,注释详细,已通过检测成功,分割合并文本文档无错误。
文本阅读器,读取文本文档阅读,文件操作,字符编码,分页处理,边界问题,等。