网站首页
公司简介
软件开发
网站建设
产品展示
经典案例
解决方案
一卡通系统
短信服务
联系我们
网站建设 主机托管 快速建站 QQ:21543821 电话:0731-4454366 手机:(0)13873166650
  您的位置:首页 >> 技术文章 >> ASP.NET

用c#操作txt文本文件的方法

来源:原创  发布人:www.ruanzhi.com  发表时间:2010-3-22  点击:  字体: 【双击滚动窗口】

以下是代码片段:

如何读取文本文件内容:

  在本文介绍的程序中,是把读取的文本文件,用一个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 ( ) ;

  }

  }

Tag标签:c# asp.net txt 文本文件 读取 写入 方法 操作
关闭窗口】  【打印此页】 
  • 上一篇文章:SQL语句实现Sql Server 2005日志收缩(批量)
  • 推荐产品
    ·短信群发软件及销售
    ·电子政务系统(标准版)
    ·流动人口管理系统(计...
    ·人力资源管理系统
    ·社区计生网格化管理系统
    ·社区综合事务管理系统
    ·网格化电子政务
    最新发布
    ·短信群发软件及销售
    ·长沙网站制作::快速建...
    ·自助型网站建设
    ·简约型网站建设
    ·实惠型网站建设
    ·标准型网站建设
    ·大型行业网站建设
    相关信息
    ·SQL语句实现Sql Server...
    ·在VB.net中如何把汉字...
    ·通过WebClient获取网页...
    ·asp中的rs.open与conn....
    ·在asp中,整除用"\"来...
    ·用C#导入EXCEL数据到...
    ·C#在WebBrowser里自动...
    服务项目: 长沙短信群发,长沙办公自动化建设,长沙网站建设,长沙网页设计,长沙人事管理系统,长沙OA办公系统,长沙软件定制开发,长沙短信平台开发,长沙邮件系统,长沙IDC托管服务,长沙SEO优化,长沙短信群发,长沙短信群发平台,长沙短信平台,长沙短信公司,长沙短信群发公司,长沙短信群发服务公司
    友情链接: 长沙食堂售饭机    湖南长沙短信群发    长沙短信群发平台    长沙短信群发公司    芙蓉区东岸乡锦林社区网    湖南数据中心    
     

    在线咨询QQ:21543821 点击这里给我发消息、895078010 点击这里给我发消息、183932503 点击这里给我发消息
    Copyrigh ® 2006-2008 长沙软智科技有限公司 版权所有       技术支持:长沙软智科技有限公司软件开发部
    地址:长沙市芙蓉区火车站凯通国际二栋三单元(天心电脑城南边)
    联系人:雷经理 电话:0731-84454366 84480976  手机:13873166650
    邮箱:ruanzhitech@126.com      湘ICP备:08103883号