首页 > 代码库 > 用递归列出指定文件夹下的所有文件

用递归列出指定文件夹下的所有文件

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;namespace ShowFiles{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Sel_File_Btn_Click(object sender, EventArgs e) { if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK) { this.listBox1.Items.Clear(); this.listBox1.Items.Add("-根目录" + folderBrowserDialog1.SelectedPath); listFiles(folderBrowserDialog1.SelectedPath); } } private void listFiles(string path) { if (Directory.GetDirectories(path).Length == 0) { string[] files = Directory.GetFiles(path); foreach (string fi in files) this.listBox1.Items.Add("----文件"+fi); } else { string[] dirs = Directory.GetDirectories(path); foreach (string dir in dirs) { this.listBox1.Items.Add("--目录"+dir); listFiles(dir); //递归调用 } } } }}