Directory+FileSteam+递归+TreeView控件实现小说阅读器

程序效果图:

 

程序关键代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.IO;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 
12 namespace WindowsFormsApplication
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void Form1_Load(object sender, EventArgs e)
22         {
23 
24             //获取小说所在目录路径
25             string appPath = AppDomain.CurrentDomain.BaseDirectory;
26             string storyDireName = "福尔摩斯探案集_柯南 ▪ 道尔";
27             string storyDirePath = Path.Combine(appPath, storyDireName);
28 
29             //使用小说文件夹名称作为根节点
30             TreeNode rootNode = treeView1.Nodes.Add(storyDireName);
31 
32             BingdingTreeView(rootNode, storyDirePath);
33  
34 
35         }
36 
37         private void BingdingTreeView(TreeNode node, string direPath)
38         {
39             //获取指定目录下的文件夹和文件
40             string[] dires = Directory.GetDirectories(direPath); //文件夹路径集合
41             string[] files = Directory.GetFiles(direPath);//文件路径集合
42 
43 
44             foreach (string dire in dires)//绑定文件夹
45             {
46                 string direName = Path.GetFileName(dire);
47                 TreeNode subNode = node.Nodes.Add(direName);
48 
49                 BingdingTreeView(subNode, dire); //递归
50             }
51 
52             foreach (string file in files)//绑定文件
53             {
54                 string fileName = Path.GetFileName(file);   
55                 TreeNode subNode = node.Nodes.Add(fileName);
56                 subNode.Tag = file;  //文件路径关联
57             }
58 
59         }// END BingdingTreeView()
60  
61 
62   
63  
64 
65         private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
66         {
67            
68             if (e.Node.Tag == null) return;
69 
70             string filePath = e.Node.Tag.ToString();
71             StringBuilder sb = new StringBuilder();
72 
73             using (Stream fsRead = new FileStream(filePath, FileMode.Open, FileAccess.Read))
74             {
75                 byte[] buffer = new byte[1024 * 5]; //每次读5kb
76                 int readByteCount = 0; //实际读到的字节数
77                 while ((readByteCount = fsRead.Read(buffer, 0, buffer.Length)) != 0)
78                 {
79                     string context = Encoding.Default.GetString(buffer, 0, readByteCount);
80                     sb.Append(context);
81                 }
82             } // END Using
83 
84             textBox1.Text = sb.ToString();
85         } 
86 
87     }
88 }

 

编写递归说明:

递归简单来说就是函数调用自己,因此在编写时很容易出现无限循环的错误。

编写递归函数时,需要实现两部分:基线条件和递归条件。递归条件值的是函数调用自己,基线条件则指的是函数不在调用自己,从而避免出现无限循环。

 

此案例的递归条件:

Directory+FileSteam+递归+TreeView控件实现小说阅读器_第1张图片

 

你可能感兴趣的:(Directory+FileSteam+递归+TreeView控件实现小说阅读器)