自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#處理文本文件淺析

開發(fā) 后端
這里介紹用C#處理文本文件的完整源程序代碼,本文雖然只是介紹了用C#處理文本文件,但其實對C#處理其他文件也有很多的參考價值。

本文向大家介紹C#處理文本文件,可能好多人還不知道C#處理文本文件,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。

用C#處理文本文件的完整源程序代碼(control.cs),現(xiàn)在就可以方便的得到用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 = "預(yù)覽" ;  
  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 = "打印機設(shè)置" ;  
  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. // 從數(shù)據(jù)流中讀取每一行,直到文件的***一行,并在richTextBox1中顯示出內(nèi)容  
  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. //關(guān)閉此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. //創(chuàng)建一個文件流,用以寫入或者創(chuàng)建一個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來往文件中寫入內(nèi)容  
  166. m_streamWriter.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;  
  167. // 把richTextBox1中的內(nèi)容寫入文件  
  168. m_streamWriter.Write ( richTextBox1.Text ) ;  
  169. //關(guān)閉此文件  
  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. //預(yù)覽打印文檔  
  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中的內(nèi)容  
  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. //重復(fù)使用StringReader對象 ,打印出richTextBox1中的所有內(nèi)容  
  226. while ( count < linesPerPage && ( ( line = myReader.ReadLine ( ) ) != null ) )  
  227. {  
  228. // 計算出要打印的下一行所基于頁面的位置  
  229. yPosition = topMargin + ( count * printFont.GetHeight ( ev.Graphics ) ) ;  
  230. // 打印出richTextBox1中的下一行內(nèi)容  
  231. ev.Graphics.DrawString ( line , printFont , myBrush , 
    leftMargin , yPosition , new StringFormat ( ) ) ;  
  232. count++ ;  
  233. }  
  234. // 判斷如果還要下一頁,則繼續(xù)打印  
  235. if ( line != null )  
  236. ev.HasMorePages = true ;  
  237. else  
  238. ev.HasMorePages = false ;  
  239. myBrush.Dispose ( ) ;  
  240. }  

本文雖然只是介紹了用C#處理文本文件,但其實對C#處理其他文件也有很多的參考價值,這是因為在名字空間"System.IO"中定義的用以處理其他文件的類的結(jié)構(gòu)和用以處理文本文件的類的結(jié)構(gòu)有很多是相同的。希望本文對你用C#進行文件方面的編程有所幫助。

【編輯推薦】

  1. C# this關(guān)鍵字詳解
  2. C#調(diào)用析構(gòu)方法詳解
  3. C#釋放托管資源簡單描述
  4. C#單路代理簡單分析
  5. C#文法產(chǎn)生式概述
責(zé)任編輯:佚名 來源: 博客園
相關(guān)推薦

2009-09-02 19:08:03

C#實現(xiàn)讀取文本文件

2009-08-06 18:33:45

C#處理文本文件

2009-08-19 17:44:15

C#操作文本文件

2009-08-20 10:17:27

C#操作文本文件

2009-08-20 09:15:20

C#操作文本文件

2009-08-26 11:53:56

C#打印文本文件

2009-08-12 17:59:48

C#讀取文本文

2009-09-04 15:56:35

寫入文本文件

2009-08-20 09:58:06

C#操作文本文件

2015-06-17 14:28:15

Java查詢處理方法

2009-08-12 17:42:57

C#讀文本文件

2009-08-20 09:26:14

C#操作文本文件

2010-02-01 14:26:50

C++讀寫文本文件

2010-04-30 17:38:31

Unix文本

2021-11-29 09:46:11

FileReaderJava開發(fā)

2010-01-15 10:05:35

VB.NET文件對象

2009-08-12 15:20:21

C#事件處理

2014-03-11 10:11:33

Linux命令more命令文本文件

2023-03-31 23:31:06

.go文本文件

2022-09-29 10:01:05

Go編程語言文本文件
點贊
收藏

51CTO技術(shù)棧公眾號