惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
O
OpenAI News
D
Docker
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
K
Kaspersky official blog
Security Latest
Security Latest
T
Tailwind CSS Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
N
News and Events Feed by Topic
aimingoo的专栏
aimingoo的专栏
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Help Net Security
Help Net Security
爱范儿
爱范儿
宝玉的分享
宝玉的分享
腾讯CDC
H
Heimdal Security Blog
Webroot Blog
Webroot Blog
AI
AI
WordPress大学
WordPress大学
Recorded Future
Recorded Future
SecWiki News
SecWiki News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
C
Check Point Blog
TaoSecurity Blog
TaoSecurity Blog
Cisco Talos Blog
Cisco Talos Blog
The Cloudflare Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - Franky
云风的 BLOG
云风的 BLOG

博客园 - Nillson

传说中的Singleton.... 设计模式--简单工厂模式 策略模式 抽象类与接口 C# 实现的一个二叉树类 回顾一个面试题 再谈代理 常见的排序方法 预定义,宏定义 连接符,数值运算与函数 复杂查询 数据库中的Index和View的理解 重载和重写 .NET中的新概念整理 4月要看的书 System.Runtime.InteropServices浅见 挂个牛人 一篇关于如何写注释的文章,值得收藏 Vistual Studio 2005到Vistual Studio 2008的版本转换问题 Visual Studio 2008 的一个Bug
采用递归的方法获得一棵树的所有叶节点
Nillson · 2008-04-24 · via 博客园 - Nillson

暴汗一个:我突然忘记了怎么写递归....
由于工作原因需要统计某一个文件夹下最底层的文件夹的名字, 立马想到上学时候学的统计叶节点的"故事"(...想起了数据结构上那个风姿绰约昏倒一大片的两脚圆规...永远的痛啊),于是敲定了用递归.
此处借用第一行...
以下是补上的功课..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace CasesExport
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }

        
/// <summary>
        
/// To open the "FolderBrowserDialog" and get the selected path
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void btnFolder_Click(object sender, EventArgs e)
        
{
            FolderBrowserDialog FBDialog 
= new FolderBrowserDialog();
            FBDialog.ShowNewFolderButton 
= true;
            FBDialog.ShowDialog();
            tbFolder.Text 
= FBDialog.SelectedPath;
        }

        
/// <summary>
        
/// To open the "OpenFileDialog" and set the txt file to record the folder names
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void btnFile_Click(object sender, EventArgs e)
        
{
            OpenFileDialog OFDialog 
= new OpenFileDialog();
            OFDialog.ShowDialog();
            tbFile.Text 
= OFDialog.FileName;
        }

        
/// <summary>
        
/// Mainly work:
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void btnOK_Click(object sender, EventArgs e)
        
{
            
if (this.tbFolder.Text.Length == 0 || this.tbFile.Text.Length == 0)
            
{
                
return;
            }

            
try
            
{
                DirectoryInfo DInfo 
= new DirectoryInfo(this.tbFolder.Text.Trim());
                ArrayList al 
= new ArrayList();
                GetLeafNodeFolder(DInfo, 
ref al);
                
string StrContent = null;
                
foreach (string folderName in al)
                
{
                    StrContent 
+= folderName + "\r\n";
                }

                File.WriteAllText(
this.tbFile.Text, StrContent);
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.ToString());
            }

            
finally
            
{
                
this.Dispose();
            }

            
        }

        
/// <summary>
        
/// Use recursion to get the leaf node and post them to an ArrayList
        
/// </summary>
        
/// <param name="dri"></param>
        
/// <param name="al"></param>

        private void GetLeafNodeFolder(DirectoryInfo dri, ref ArrayList al)
        
{
            
if (dri.GetDirectories().Length == 0)
            
{
                al.Add(dri.Name);
            }

            
else
            
{
                
foreach (DirectoryInfo subDri in dri.GetDirectories())
                
{
                    GetLeafNodeFolder(subDri, 
ref al);
                }

            }

        }


        
private void btnCancle_Click(object sender, EventArgs e)
        
{
            
this.Dispose();
        }

    }

}

试图用循环的方法来解决这个问题,但是未能理出很好的头绪,主要是无法确定一个未知文件夹到底有多"深", 看来在这些最基本的问题上还是需要加强的.