C#处理文本文件的方法

发布时间:2021-07-16 10:58:39 作者:chen
阅读:138
开发者专用服务器限时活动,0元免费领! 查看>>

本篇内容主要讲解“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. public Form1 ( )  

  25. {  

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

  27. InitializeComponent ( ) ;  

  28. }  

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

  30. protected override void Dispose ( bool disposing )  

  31. {  

  32. if ( disposing )  

  33. {  

  34. if ( components != null )  

  35. {  

  36. components.Dispose ( ) ;  

  37. }  

  38. }  

  39. base.Dispose ( disposing ) ;  

  40. }  

  41. private void InitializeComponent ( )  

  42. {  

  43. richTextBox1 = new RichTextBox ( ) ;  

  44. button1 = new Button ( ) ;  

  45. button2 = new Button ( ) ;  

  46. button3 = new Button ( ) ;  

  47. button4 = new Button ( ) ;  

  48. button5 = new Button ( ) ;  

  49. saveFileDialog1 = new SaveFileDialog ( ) ;  

  50. openFileDialog1 = new OpenFileDialog ( ) ;  

  51. printPreviewDialog1 = new PrintPreviewDialog ( ) ;  

  52. printDialog1 = new PrintDialog ( ) ;  

  53. ThePrintDocument = new PrintDocument ( ) ;  

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

  55. SuspendLayout ( ) ;  

  56. richTextBox1.Anchor = AnchorStyles.None ;  

  57. richTextBox1.Name = "richTextBox1" ;  

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

  59. richTextBox1.TabIndex = 0 ;  

  60. richTextBox1.Text = "" ;  

  61. button1.Anchor = AnchorStyles.None ;  

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

  63. button1.Name = "button1" ;  

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

  65. button1.TabIndex = 1 ;  

  66. button1.Text = "打开" ;  

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

  68. button2.Anchor = AnchorStyles.None ;  

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

  70. button2.Name = "button2" ;  

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

  72. button2.TabIndex = 4 ;  

  73. button2.Text = "预览" ;  

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

  75. button3.Anchor = AnchorStyles.None ;  

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

  77. button3.Name = "button3" ;  

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

  79. button3.TabIndex = 2 ;  

  80. button3.Text = "保存" ;  

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

  82. button4.Anchor = AnchorStyles.None ;  

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

  84. button4.Name = "button4" ;  

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

  86. button4.TabIndex = 3 ;  

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

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

  89. button5.Anchor = AnchorStyles.None ;  

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

  91. button5.Name = "button5" ;  

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

  93. button5.TabIndex = 5 ;  

  94. button5.Text = "打印" ;  

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

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

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

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

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

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

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

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

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

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

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

  106. this.Controls.Add ( button1 ) ;  

  107. this.Controls.Add ( button2 ) ;  

  108. this.Controls.Add ( button3 ) ;  

  109. this.Controls.Add ( button4 ) ;  

  110. this.Controls.Add ( button5 ) ;  

  111. this.Controls.Add ( richTextBox1 ) ;  

  112. this.MaximizeBox = false ;  

  113. this.Name = "Form1" ;  

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

  115. this.ResumeLayout ( false ) ;  

  116. }  

  117. static void Main ( )  

  118. {  

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

  120. }  

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

  122. {  

  123. try  

  124. {  

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

  126. {  

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

  128. StreamReader m_streamReader = new StreamReader ( fs ) ;  

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

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

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

  132. this.richTextBox1.Text = "" ;  

  133. string strLine = m_streamReader.ReadLine ( ) ;  

  134. while ( strLine != null )  

  135. {  

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

  137. strLine = m_streamReader.ReadLine ( ) ;  

  138. }  

  139. //关闭此StreamReader对象  

  140. m_streamReader.Close ( ) ;  

  141. }  

  142. }  

  143. catch ( Exception em )  

  144. {  

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

  146. }  

  147. }  

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

  149. {  

  150. try  

  151. {  

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

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

  154. {  

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

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

  157. StreamWriter m_streamWriter = new StreamWriter ( fs ) ;  

  158. m_streamWriter.Flush ( ) ;  

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

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

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

  162. m_streamWriter.Write ( richTextBox1.Text ) ;  

  163. //关闭此文件  

  164. m_streamWriter.Flush ( ) ;  

  165. m_streamWriter.Close ( ) ;  

  166. }  

  167. }  

  168. catch ( Exception em )  

  169. {  

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

  171. }  

  172. }  

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

  174. {  

  175. printDialog1.Document = ThePrintDocument ;  

  176. printDialog1.ShowDialog ( ) ;  

  177. }  

  178. //预览打印文档  

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

  180. {  

  181. try  

  182. {  

  183. string strText = richTextBox1.Text ;  

  184. myReader = new StringReader ( strText ) ;  

  185. PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ( ) ;  

  186. printPreviewDialog1.Document = ThePrintDocument;  

  187. printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;  

  188. printPreviewDialog1.ShowDialog ( ) ;  

  189. }  

  190. catch ( Exception exp )  

  191. {  

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

  193. }  

  194. }  

  195. //打印richTextBox1中的内容  

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

  197. {  

  198. printDialog1.Document = ThePrintDocument ;  

  199. string strText = richTextBox1.Text ;  

  200. myReader = new StringReader ( strText ) ;  

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

  202. {  

  203. ThePrintDocument.Print ( ) ;  

  204. }  

  205. }  

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

  207. {  

  208. float linesPerPage = 0 ;  

  209. float yPosition = 0 ;  

  210. int count = 0 ;  

  211. float leftMargin = ev.MarginBounds.Left ;  

  212. float topMargin = ev.MarginBounds.Top ;  

  213. string line = null ;  

  214. Font printFont = richTextBox1.Font ;  

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

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

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

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

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

  220. {  

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

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

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

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

  225. count++ ;  

  226. }  

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

  228. if ( line != null )  

  229. ev.HasMorePages = true ;  

  230. else  

  231. ev.HasMorePages = false ;  

  232. myBrush.Dispose ( ) ;  

  233. }  

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

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

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

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

开发者交流群:

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

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

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

相关阅读

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

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