暴汗一个:我突然忘记了怎么写递归....
由于工作原因需要统计某一个文件夹下最底层的文件夹的名字, 立马想到上学时候学的统计叶节点的"故事"(...想起了数据结构上那个风姿绰约昏倒一大片的两脚圆规...永远的痛啊),于是敲定了用递归.
此处借用第一行...
以下是补上的功课..
试图用循环的方法来解决这个问题,但是未能理出很好的头绪,主要是无法确定一个未知文件夹到底有多"深", 看来在这些最基本的问题上还是需要加强的.
由于工作原因需要统计某一个文件夹下最底层的文件夹的名字, 立马想到上学时候学的统计叶节点的"故事"(...想起了数据结构上那个风姿绰约昏倒一大片的两脚圆规...永远的痛啊),于是敲定了用递归.
此处借用第一行...
以下是补上的功课..
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();
}
}
}
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();
}
}
}
试图用循环的方法来解决这个问题,但是未能理出很好的头绪,主要是无法确定一个未知文件夹到底有多"深", 看来在这些最基本的问题上还是需要加强的.