真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

C#處理文本文件的方法

本篇內(nèi)容主要講解“C#處理文本文件的方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C#處理文本文件的方法”吧!

成都創(chuàng)新互聯(lián)公司成立與2013年,先為新津縣等服務(wù)建站,新津縣等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為新津縣企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

用C#處理文本文件的完整源程序代碼(control.cs),現(xiàn)在就可以方便的得到用C#處理文本文件的一個(gè)完整的源程序,具體如下:

  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. //初始化窗體中的各個(gè)組件  

  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 = "打開(kāi)" ;  

  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 = "打印機(jī)設(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 = "打開(kāi)文本文件!" ;  

  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#來(lái)操作文本文件" ;  

  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類(lèi)來(lái)讀取文件  

  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對(duì)象  

  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. //獲得另存為的文件名稱(chēng)  

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

  159. {  

  160. //創(chuàng)建一個(gè)文件流,用以寫(xiě)入或者創(chuàng)建一個(gè)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來(lái)往文件中寫(xiě)入內(nèi)容  

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

  167. // 把richTextBox1中的內(nèi)容寫(xiě)入文件  

  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. //計(jì)算每一頁(yè)打印多少行  

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

  225. //重復(fù)使用StringReader對(duì)象 ,打印出richTextBox1中的所有內(nèi)容  

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

  227. {  

  228. // 計(jì)算出要打印的下一行所基于頁(yè)面的位置  

  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. // 判斷如果還要下一頁(yè),則繼續(xù)打印  

  235. if ( line != null )  

  236. ev.HasMorePages = true ;  

  237. else  

  238. ev.HasMorePages = false ;  

  239. myBrush.Dispose ( ) ;  

  240. }  

本文雖然只是介紹了用C#處理文本文件,但其實(shí)對(duì)C#處理其他文件也有很多的參考價(jià)值,這是因?yàn)樵诿挚臻g"System.IO"中定義的用以處理其他文件的類(lèi)的結(jié)構(gòu)和用以處理文本文件的類(lèi)的結(jié)構(gòu)有很多是相同的。希望本文對(duì)你用C#進(jìn)行文件方面的編程有所幫助。

到此,相信大家對(duì)“C#處理文本文件的方法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!


當(dāng)前文章:C#處理文本文件的方法
當(dāng)前路徑:http://weahome.cn/article/gpgsis.html

其他資訊

在線咨詢(xún)

微信咨詢(xún)

電話咨詢(xún)

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部