zoukankan      html  css  js  c++  java
  • 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 }

    编写递归说明:

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

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

    此案例的递归条件:

  • 相关阅读:
    手把手带你玩转 DialogFragment
    紧张的去京东面试7,没想到可以成功拿下offer
    这个有点强,MySQL常用优化指南及大表优化思路(值得收藏)
    Java程序员两年经验斩获头条 Offer,技术杠杠的
    为什么大家都说 SELECT * 效率低
    Java程序员想要靠外包刷题,结果却大跌眼镜,心态都崩了
    一次请求在SpringMVC核心执行流程
    工作三年经验,一年内我靠这份javaBAT进阶面试题从13K到大厂25K
    用了这么久的数据库连接池,你知道原理吗?
    poj 3295 Tautology(栈)
  • 原文地址:https://www.cnblogs.com/green-jcx/p/13162909.html
Copyright © 2011-2022 走看看