C#处理文本文件的方法

发布时间:2021-07-16 10:58:39 作者:chen
来源:亿速云 阅读:126

本篇内容主要讲解“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#处理文本文件的方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. python的文本文件处理介绍i
  2. 处理任意格式的文本文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:如何将execel表格的数据导入到mysql数据库

下一篇:Web开发中客户端跳转与服务器端跳转有什么区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》