1. 程式人生 > >C#簡單的檔案閱讀器

C#簡單的檔案閱讀器

寫一個簡單的檔案閱讀器 

1、可以讀取大檔案(2G)
2、實現首頁、下一頁、前一頁、末頁的跳轉
3、實現到指定頁面的跳轉,比如跳轉到第**頁
4、限制每頁顯示字元數 1029-4069byte,且使用者可自定義該值
5、介面要能實時響應
6、使用者可以建立自己的txt檔案
7、介面可儘可能的簡單

第一步:畫介面

第二步:相應事件

上程式碼

  1 using System;
  2 using System.IO;
  3 using System.Text;
  4 using System.Windows.Forms;
  5 
  6 namespace
txtReader 7 { 8 public partial class Form1 : Form 9 { 10 private static int PageTotal = 0; 11 private static int PageCurrent = 0; 12 public Form1() 13 { 14 InitializeComponent(); 15 } 16 17 /// <summary> 18
/// 開啟檔案 19 /// </summary> 20 /// <param name="sender"></param> 21 /// <param name="e"></param> 22 private void TsMenuOpen_Click(object sender, EventArgs e) 23 { 24 if (ofd.ShowDialog() == DialogResult.OK) 25 {
26 PageCurrent = 0; 27 ReadTxt(); 28 } 29 } 30 31 private void ReadTxt() 32 { 33 byte[] byts = new byte[decimal.ToInt32(numericUpDown2.Value)]; 34 using (var fs = File.OpenRead(ofd.FileName)) 35 { 36 int MValue = decimal.ToInt32(numericUpDown2.Value); 37 int ValueFrom = MValue * PageCurrent; 38 fs.Position = ValueFrom; 39 fs.Read(byts, 0, MValue); 40 if (byts != null) 41 { 42 int v = (int)((fs.Length / decimal.ToInt32(numericUpDown2.Value)) + 1); 43 PageTotal = v; 44 txtShow.Text = Encoding.Default.GetString(byts); 45 } 46 fs.Close(); 47 } 48 } 49 50 /// <summary> 51 /// 儲存檔案 52 /// </summary> 53 /// <param name="sender"></param> 54 /// <param name="e"></param> 55 private void TxMenuSave_Click(object sender, EventArgs e) 56 { 57 SaveFileDialog sfd = new SaveFileDialog(); 58 sfd.Filter = "文字檔案|*.txt"; 59 if (DialogResult.OK == sfd.ShowDialog()) 60 { 61 using (StreamWriter sw = new StreamWriter(sfd.FileName, false, Encoding.Default)) 62 { 63 sw.WriteLine(txtShow.Text.Trim()); 64 sw.Close(); 65 } 66 } 67 } 68 /// <summary> 69 /// 關閉檔案 70 /// </summary> 71 /// <param name="sender"></param> 72 /// <param name="e"></param> 73 private void TxMenuClose_Click(object sender, EventArgs e) 74 { 75 this.Close(); 76 } 77 /// <summary> 78 /// 首頁 79 /// </summary> 80 /// <param name="sender"></param> 81 /// <param name="e"></param> 82 private void BtnFirst_Click(object sender, EventArgs e) 83 { 84 PageCurrent = 0; 85 ReadTxt(); 86 } 87 /// <summary> 88 /// 上一頁 89 /// </summary> 90 /// <param name="sender"></param> 91 /// <param name="e"></param> 92 private void BtnPagePre_Click(object sender, EventArgs e) 93 { 94 PageCurrent = PageCurrent > 0 ? PageCurrent - 1 : 0; 95 ReadTxt(); 96 } 97 /// <summary> 98 /// 下一頁 99 /// </summary> 100 /// <param name="sender"></param> 101 /// <param name="e"></param> 102 private void BtnPageNext_Click(object sender, EventArgs e) 103 { 104 PageCurrent++; 105 ReadTxt(); 106 } 107 /// <summary> 108 /// 尾頁 109 /// </summary> 110 /// <param name="sender"></param> 111 /// <param name="e"></param> 112 private void BtnPageEnd_Click(object sender, EventArgs e) 113 { 114 PageCurrent = PageTotal - 1; 115 ReadTxt(); 116 } 117 /// <summary> 118 /// 跳轉 119 /// </summary> 120 /// <param name="sender"></param> 121 /// <param name="e"></param> 122 private void BtnPageTo_Click(object sender, EventArgs e) 123 { 124 PageCurrent = decimal.ToInt32(numericUpDown1.Value); 125 ReadTxt(); 126 } 127 } 128 }
View Code

總結

這個小功能難點只用這個

確定讀取檔案的位置,和每次只讀指定部分字元

 1 using (var fs = File.OpenRead(ofd.FileName))
 2 {
 3     int MValue = decimal.ToInt32(numericUpDown2.Value);
 4     int ValueFrom = MValue * PageCurrent;
 5     fs.Position = ValueFrom;
 6     fs.Read(byts, 0, MValue);
 7     if (byts != null)
 8     {
 9         int v = (int)((fs.Length / decimal.ToInt32(numericUpDown2.Value)) + 1);
10         PageTotal = v;
11         txtShow.Text = Encoding.Default.GetString(byts);
12     }
13     fs.Close();
14 }
View Code

 

 最後附上專案下載原始碼txtReader.zip