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

猜你喜欢

转载自www.cnblogs.com/sunbingqiang/p/10056641.html