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 WindowsFormsApplication68
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Clear(); //清空ListView控件中的项
if(folderBrowserDialog1.ShowDialog()==DialogResult.OK) //判断是否选择了路径
{
textBox1.Text=folderBrowserDialog1.SelectedPath; //显示选择的路径
DirectoryInfo dinfo = new DirectoryInfo(textBox1.Text); //创建DirectoryInfo对象
//获取指定目录下的所有子目录及文件类型
FileSystemInfo[] fsinfos=dinfo.GetFileSystemInfos();
foreach(FileSystemInfo fsinfo in fsinfos)
{
if(fsinfo is DirectoryInfo) //判断是否是文件夹
{
//使用获取的文件夹名称创建DirectoryInfo对象
DirectoryInfo dirinfo = new DirectoryInfo(fsinfo.FullName);
listView1.Items.Add(dirinfo.Name); //为ListView控件添加文件夹信息
//显示文件夹名称
listView1.Items[listView1.Items.Count - 1].SubItems.Add(dirinfo.FullName);
//显示一个空字符串
listView1.Items[listView1.Items.Count - 1].SubItems.Add("");
//显示文件夹创建时间
listView1.Items[listView1.Items.Count - 1].SubItems.Add(dirinfo.CreationTime.ToShortDateString());
}
else
{
FileInfo finfo = new FileInfo(fsinfo.FullName); //使用获取的文件名称创建FileInfo对象
listView1.Items.Add(finfo.Name); //为ListView控件添加文件信息
//显示文件夹名称
listView1.Items[listView1.Items.Count - 1].SubItems.Add(finfo.FullName);
//显示文件夹大小
listView1.Items[listView1.Items.Count - 1].SubItems.Add(finfo.Length.ToString());
//显示文件夹创建时间
listView1.Items[listView1.Items.Count - 1].SubItems.Add(finfo.CreationTime.ToShortDateString());
}
}
}
}
}
}