假设a文件夹在F盘下,代码如下。将文件名输出到一个ListBox中
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
DirectoryInfo theFolder = new DirectoryInfo(@"F:\a\");
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
//遍历文件夹
foreach (DirectoryInfo NextFolder in dirInfo)
{
// this.listBox1.Items.Add(NextFolder.Name);
FileInfo[] fileInfo = NextFolder.GetFiles();
foreach (FileInfo NextFile in fileInfo) //遍历文件
this.listBox2.Items.Add(NextFile.Name);
}