广告
返回顶部
首页 > 资讯 > 精选 >C#处理文本文件的方法
  • 171
分享到

C#处理文本文件的方法

2023-06-17 22:06:33 171人浏览 泡泡鱼
摘要

本篇内容主要讲解“C#处理文本文件的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#处理文本文件的方法”吧!用C#处理文本文件的完整源程序代码(control.cs),现在就可以方便的得到

本篇内容主要讲解“C#处理文本文件的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#处理文本文件的方法”吧!

用C#处理文本文件的完整源程序代码(control.cs),现在就可以方便的得到用C#处理文本文件的一个完整的源程序,具体如下:

  1. using System ;  

  2. using System.Drawing ;  

  3. using System.Collections ;  

  4. using System.ComponentModel ;  

  5. using System.windows.FORMs ;  

  6. using System.Data ;  

  7. using System.IO ;  

  8. using System.Drawing.Printing ;  

  9. public class Form1 : Form  

  10. {  

  11. private RichTextBox richTextBox1 ;  

  12. private Button button1 ;  

  13. private Button button2 ;  

  14. private Button button3 ;  

  15. private Button button4 ;  

  16. private Button button5 ;  

  17. private OpenFileDialog openFileDialog1 ;  

  18. private SaveFileDialog saveFileDialog1 ;  

  19. private PrintDialog printDialog1 ;  

  20. private PrintDocument ThePrintDocument ;  

  21. private PrintPreviewDialog printPreviewDialog1 ;  

  22. private StringReader myReader ;  

  23. private System.ComponentModel.Container components = null ;  

  24.  

  25. public Form1 ( )  

  26. {  

  27. //初始化窗体中的各个组件  

  28. InitializeComponent ( ) ;  

  29. }  

  30. //清除程序中使用多的资源  

  31. protected override void Dispose ( bool disposing )  

  32. {  

  33. if ( disposing )  

  34. {  

  35. if ( components != null )  

  36. {  

  37. components.Dispose ( ) ;  

  38. }  

  39. }  

  40. base.Dispose ( disposing ) ;  

  41. }  

  42. private void InitializeComponent ( )  

  43. {  

  44. richTextBox1 = new RichTextBox ( ) ;  

  45. button1 = new Button ( ) ;  

  46. button2 = new Button ( ) ;  

  47. button3 = new Button ( ) ;  

  48. button4 = new Button ( ) ;  

  49. button5 = new Button ( ) ;  

  50. saveFileDialog1 = new SaveFileDialog ( ) ;  

  51. openFileDialog1 = new OpenFileDialog ( ) ;  

  52. printPreviewDialog1 = new PrintPreviewDialog ( ) ;  

  53. printDialog1 = new PrintDialog ( ) ;  

  54. ThePrintDocument = new PrintDocument ( ) ;  

  55. ThePrintDocument.PrintPage += new PrintPageEventHandler 
    ( ThePrintDocument_PrintPage ) ;  

  56. SuspendLayout ( ) ;  

  57. richTextBox1.Anchor = AnchorStyles.None ;  

  58. richTextBox1.Name = "richTextBox1" ;  

  59. richTextBox1.Size = new Size ( 448 , 280 ) ;  

  60. richTextBox1.TabIndex = 0 ;  

  61. richTextBox1.Text = "" ;  

  62. button1.Anchor = AnchorStyles.None ;  

  63. button1.Location = new Point ( 41 , 289 ) ;  

  64. button1.Name = "button1" ;  

  65. button1.Size = new Size ( 48 , 30 ) ;  

  66. button1.TabIndex = 1 ;  

  67. button1.Text = "打开" ;  

  68. button1.Click += new System.EventHandler ( button1_Click ) ;  

  69. button2.Anchor = AnchorStyles.None ;  

  70. button2.Location = new Point ( 274 , 288 ) ;  

  71. button2.Name = "button2" ;  

  72. button2.Size = new Size ( 48 , 30 ) ;  

  73. button2.TabIndex = 4 ;  

  74. button2.Text = "预览" ;  

  75. button2.Click += new System.EventHandler ( button2_Click ) ;  

  76. button3.Anchor = AnchorStyles.None ;  

  77. button3.Location = new Point ( 108 , 288 ) ;  

  78. button3.Name = "button3" ;  

  79. button3.Size = new Size ( 48 , 30 ) ;  

  80. button3.TabIndex = 2 ;  

  81. button3.Text = "保存" ;  

  82. button3.Click += new System.EventHandler ( button3_Click ) ;  

  83. button4.Anchor = AnchorStyles.None ;  

  84. button4.Location = new Point ( 174 , 288 ) ;  

  85. button4.Name = "button4" ;  

  86. button4.Size = new Size ( 80 , 30 ) ;  

  87. button4.TabIndex = 3 ;  

  88. button4.Text = "打印机设置" ;  

  89. button4.Click += new System.EventHandler ( button4_Click ) ;  

  90. button5.Anchor = AnchorStyles.None ;  

  91. button5.Location = new Point ( 345 , 288 ) ;  

  92. button5.Name = "button5" ;  

  93. button5.Size = new Size ( 48 , 30 ) ;  

  94. button5.TabIndex = 5 ;  

  95. button5.Text = "打印" ;  

  96. button5.Click += new System.EventHandler ( button5_Click ) ;  

  97. saveFileDialog1.DefaultExt = "*.txt" ;  

  98. saveFileDialog1.FileName = "file.txt" ;  

  99. saveFileDialog1.InitialDirectory = "c:\\" ;  

  100. saveFileDialog1.Title = "另存为!" ;  

  101. openFileDialog1.DefaultExt = "*.txt" ;  

  102. openFileDialog1.FileName = "file.txt" ;  

  103. openFileDialog1.InitialDirectory = "c:\\" ;  

  104. openFileDialog1.Title = "打开文本文件!" ;  

  105. AutoScaleBaseSize = new Size ( 6 , 14 ) ;  

  106. ClientSize = new Size ( 448 , 325 ) ;  

  107. this.Controls.Add ( button1 ) ;  

  108. this.Controls.Add ( button2 ) ;  

  109. this.Controls.Add ( button3 ) ;  

  110. this.Controls.Add ( button4 ) ;  

  111. this.Controls.Add ( button5 ) ;  

  112. this.Controls.Add ( richTextBox1 ) ;  

  113.  

  114. this.MaximizeBox = false ;  

  115. this.Name = "Form1" ;  

  116. this.Text = "C#来操作文本文件" ;  

  117. this.ResumeLayout ( false ) ;  

  118. }  

  119. static void Main ( )  

  120. {  

  121. Application.Run ( new Form1 ( ) ) ;  

  122. }  

  123.  

  124. private void button1_Click ( object sender , System.EventArgs e )  

  125. {  

  126. try  

  127. {  

  128. if ( openFileDialog1.ShowDialog ( ) == DialogResult.OK )  

  129. {  

  130. FileStream fs = new FileStream ( openFileDialog1.FileName, 
    FileMode.Open , FileAccess.Read ) ;  

  131. StreamReader m_streamReader = new StreamReader ( fs ) ;  

  132. //使用StreamReader类来读取文件  

  133. m_streamReader.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;  

  134. // 从数据流中读取每一行,直到文件的***一行,并在richTextBox1中显示出内容  

  135. this.richTextBox1.Text = "" ;  

  136. string strLine = m_streamReader.ReadLine ( ) ;  

  137. while ( strLine != null )  

  138. {  

  139. this.richTextBox1.Text += strLine + "\n" ;  

  140. strLine = m_streamReader.ReadLine ( ) ;  

  141. }  

  142. //关闭此StreamReader对象  

  143. m_streamReader.Close ( ) ;  

  144. }  

  145. }  

  146. catch ( Exception em )  

  147. {  

  148. Console.WriteLine ( em.Message.ToString ( ) ) ;  

  149. }  

  150.  

  151. }  

  152.  

  153. private void button3_Click ( object sender , System.EventArgs e )  

  154. {  

  155. try  

  156. {  

  157. //获得另存为的文件名称  

  158. if ( saveFileDialog1.ShowDialog ( ) == DialogResult.OK )  

  159. {  

  160. //创建一个文件流,用以写入或者创建一个StreamWriter  

  161. FileStream fs = new FileStream ( @saveFileDialog1.FileName, 
    FileMode.OpenOrCreate , FileAccess.Write ) ;  

  162. StreamWriter m_streamWriter = new StreamWriter ( fs ) ;  

  163. m_streamWriter.Flush ( ) ;  

  164.  

  165. // 使用StreamWriter来往文件中写入内容  

  166. m_streamWriter.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;  

  167. // 把richTextBox1中的内容写入文件  

  168. m_streamWriter.Write ( richTextBox1.Text ) ;  

  169. //关闭此文件  

  170. m_streamWriter.Flush ( ) ;  

  171. m_streamWriter.Close ( ) ;  

  172. }  

  173. }  

  174. catch ( Exception em )  

  175. {  

  176. Console.WriteLine ( em.Message.ToString ( ) ) ;  

  177. }  

  178. }  

  179.  

  180. private void button4_Click ( object sender , System.EventArgs e )  

  181. {  

  182. printDialog1.Document = ThePrintDocument ;  

  183. printDialog1.ShowDialog ( ) ;  

  184. }  

  185. //预览打印文档  

  186. private void button2_Click ( object sender , System.EventArgs e )  

  187. {  

  188. try  

  189. {  

  190. string strText = richTextBox1.Text ;  

  191. myReader = new StringReader ( strText ) ;  

  192. PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ( ) ;  

  193. printPreviewDialog1.Document = ThePrintDocument;  

  194. printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;  

  195. printPreviewDialog1.ShowDialog ( ) ;  

  196. }  

  197. catch ( Exception exp )  

  198. {  

  199. Console.WriteLine ( exp.Message.ToString ( ) ) ;  

  200. }  

  201. }  

  202. //打印richTextBox1中的内容  

  203. private void button5_Click ( object sender , System.EventArgs e )  

  204. {  

  205. printDialog1.Document = ThePrintDocument ;  

  206. string strText = richTextBox1.Text ;  

  207. myReader = new StringReader ( strText ) ;  

  208. if ( printDialog1.ShowDialog ( ) == DialogResult.OK )  

  209. {  

  210. ThePrintDocument.Print ( ) ;  

  211. }  

  212. }  

  213. protected void ThePrintDocument_PrintPage ( object sender , PrintPageEventArgs ev )  

  214. {  

  215. float linesPerPage = 0 ;  

  216. float yPosition = 0 ;  

  217. int count = 0 ;  

  218. float leftMargin = ev.MarginBounds.Left ;  

  219. float topMargin = ev.MarginBounds.Top ;  

  220. string line = null ;  

  221. Font printFont = richTextBox1.Font ;  

  222. SolidBrush myBrush = new SolidBrush ( Color.Black ) ;  

  223. //计算每一页打印多少行  

  224. linesPerPage = ev.MarginBounds.Height / printFont.GetHeight ( ev.Graphics ) ;  

  225. //重复使用StringReader对象 ,打印出richTextBox1中的所有内容  

  226. while ( count < linesPerPage && ( ( line = myReader.ReadLine ( ) ) != null ) )  

  227. {  

  228. // 计算出要打印的下一行所基于页面的位置  

  229. yPosition = topMargin + ( count * printFont.GetHeight ( ev.Graphics ) ) ;  

  230. // 打印出richTextBox1中的下一行内容  

  231. ev.Graphics.DrawString ( line , printFont , myBrush , 
    leftMargin , yPosition , new StringFormat ( ) ) ;  

  232. count++ ;  

  233. }  

  234. // 判断如果还要下一页,则继续打印  

  235. if ( line != null )  

  236. ev.HasMorePages = true ;  

  237. else  

  238. ev.HasMorePages = false ;  

  239. myBrush.Dispose ( ) ;  

  240. }  

本文虽然只是介绍了用C#处理文本文件,但其实对C#处理其他文件也有很多的参考价值,这是因为在名字空间"System.IO"中定义的用以处理其他文件的类的结构和用以处理文本文件的类的结构有很多是相同的。希望本文对你用C#进行文件方面的编程有所帮助。

到此,相信大家对“C#处理文本文件的方法”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: C#处理文本文件的方法

本文链接: https://www.lsjlt.com/news/293909.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C#处理文本文件的方法
    本篇内容主要讲解“C#处理文本文件的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#处理文本文件的方法”吧!用C#处理文本文件的完整源程序代码(control.cs),现在就可以方便的得到...
    99+
    2023-06-17
  • C#中怎么处理文本文件
    这期内容当中小编将会给大家带来有关C#中怎么处理文本文件,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。C#处理文本文件C#处理文本文件是一种常用的文件格式,所以如何处理文本文件也就成为编程的一个重点。本文...
    99+
    2023-06-18
  • shell文件处理笔记之插入文本到文件的方法
    前言 众所周知,如果在大多数服务器上,不会预先装上 ruby 或者 python 这些语言。这时,你要想操作一些文件,一个合理的选择就是使用 shell。也就是你在 terminal中输入的那些指令。本文给...
    99+
    2022-06-04
    文件 文本 笔记
  • C#读取文本文件时字符编码的处理方式
    本篇内容介绍了“C#读取文本文件时字符编码的处理方式”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!字符编码的问题是经常会碰到的,那么在C#读...
    99+
    2023-06-18
  • C#读写文本文件(.txt)的方法实例
    读取txt文件 如果你要读取的文件内容不是很多,可以使用 File.ReadAllText(filePath) 或指定编码方式 File.ReadAllText(FilePath...
    99+
    2022-11-12
  • Python处理文本文件中控制字符的方法
    控制字符 控制字符(Control Character),或者说非打印字符,出现于特定的信息文本中,表示某一控制功能的字符,如控制符:LF(换行)、CR(回车)、FF(换页)、DEL(删除)、BS(退格)...
    99+
    2022-06-04
    文本文件 字符 方法
  • Python脚本实时处理log文件的方法
    这个Python脚本是用来对实时文件的内容监控,比如 Error 或者 time out 字段都可以进行自定义;算是我的第一个真正的Python脚本,自己感觉还是比较臃肿,不过打算放到blog上记录一下(还...
    99+
    2022-06-04
    脚本 实时 文件
  • C#操作文本文件的实现方式
    本篇内容主要讲解“C#操作文本文件的实现方式”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#操作文本文件的实现方式”吧!C#操作文本文件是如何实现的呢?让我们开始讲述吧:using ...
    99+
    2023-06-17
  • autoexec.bat——批处理文件的方法
    "autoexec.bat"是一个批处理文件,它在DOS操作系统中用于自动执行命令。下面是一些使用autoexec.bat文件的方法...
    99+
    2023-09-15
    方法
  • DOS批处理文件的方法
    本篇内容主要讲解“DOS批处理文件的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“DOS批处理文件的方法”吧!DOS下的可执行文件有三种,分别是EXE,COM和BAT。其中,EXE和COM文...
    99+
    2023-06-09
  • Python编解码问题及文本文件处理方法详解
    编解码器 在字符与字节之间的转换过程称为编解码,Python自带了超过100种编解码器,比如: ascii(英文体系) gb2312(中文体系) utf-...
    99+
    2022-11-12
  • python文件处理笔记之文本文件
    目录1. 建立文件1.1 文本文件代码实现1.2 代码编写分析2. 基本的读写文件2.1 用文件对象write(s)方法写内容2.2 用文件对象read()方法读取内容2.3 连续用...
    99+
    2022-11-12
  • python的文本处理方法有哪些
    今天小编给大家分享一下python的文本处理方法有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。1、字符串常量1.1、定...
    99+
    2023-06-04
  • python之文件操作(txt文本的读取写入及处理方法)
    目录 1. 读取txt文档 1.1 打开.txt文件逐行读取文件内容 2.1 分割句子保存列表 2.2 filter使用:使用filter可将列表中的元素进行函数处理(append和expend区别) 3.1 获取列表中每个元素的长度: 拓...
    99+
    2023-09-16
    python 开发语言
  • C#读取文件的方法
    本篇内容介绍了“C#读取文件的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!C#读取文件高效方法问题的提出:你平时是怎么读取文件的?使用...
    99+
    2023-06-18
  • VB.NET文件处理数据的方法
    本篇内容介绍了“VB.NET文件处理数据的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!VB.NET文件处理代码:'打开csv文...
    99+
    2023-06-17
  • webpack对html文件的处理方法
    这篇文章主要讲解了“webpack对html文件的处理方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“webpack对html文件的处理方法”吧!   ...
    99+
    2022-10-19
  • VScode中添加头文件和源文件(C/C++)的方法
    目录一.在相同文件夹下二.在不同文件夹下总结一.在相同文件夹下 在正常情况下,若同一文件夹下若头文件、源文件、和主要代码在同一文件夹下,则可以正常运行程序。 如图(此为Visual ...
    99+
    2022-11-13
    vscode如何添加头文件 vs添加头文件与源文件 vs code添加头文件
  • Python脚本文件外部传递参数的处理方法
    目录前言一、流文本处理二、argparse 模块三、argv 列表四、argv + argparse前言 我们写的 Python 脚本需要修改参数,当我们将脚步上传到服务器或者给不会编程的人用时,会比较不方便,如果直...
    99+
    2022-06-02
    Python脚本外部传递参数 Python传递参数 Python 外部指定参数
  • Python处理文本数据的方法详解
    目录前言用python处理文本数据用python处理数值型数据前言 HI,好久不见,今天是关闭朋友圈的第60天,我是野蛮成长的AC-Asteroid。 人生苦短,我用Python,通...
    99+
    2022-11-11
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作