利用VS创建一个winform应用程序,遍历指定文件夹(photos)内的所有文件夹及其文件。具体程序如下:
namespace 遍历文件夹及其所有文件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo TheFolder = new DirectoryInfo(@"C:Usersi-darrelDesktopphotos");
//遍历文件夹
foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
this.listBox1.Items.Add(NextFolder.Name);
//遍历文件
foreach (FileInfo NextFile in TheFolder.GetFiles())
this.listBox2.Items.Add(NextFile.Name);
}
}
}