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

推荐订阅源

V2EX - 技术
V2EX - 技术
L
LangChain Blog
IT之家
IT之家
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
U
Unit 42
B
Blog RSS Feed
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
T
Threatpost
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
Vercel News
Vercel News
S
Schneier on Security
Spread Privacy
Spread Privacy
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 叶小钗
雷峰网
雷峰网
博客园_首页
人人都是产品经理
人人都是产品经理
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
T
Tor Project blog
L
Lohrmann on Cybersecurity
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy International News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tenable Blog
V
Vulnerabilities – Threatpost
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
V
V2EX
Security Latest
Security Latest
A
About on SuperTechFans
Cloudbric
Cloudbric
S
Security Affairs
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
TaoSecurity Blog
TaoSecurity 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();
        }

    }

}

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