public void ListFiles(FileSystemInfo info)
{
if (!info.Exists) return;
DirectoryInfo dir = info as DirectoryInfo;
//不是目录
if (dir == null) return;
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
//是文件
if (file != null)
{
//Console.WriteLine(file.FullName + "\t " + file.Length);
if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".jpg?http://www.xvna.com")
//此处为显示JPG格式,不加IF可遍历所有格式的文件
{
this.list1.Items.Add(file);
//MessageBox.Show(file.FullName.Substring(file.FullName.LastIndexOf(".")));
}
}
//对于子目录,进行递归调用
else
{
ListFiles(files[i]);
}
}
}
调用:
string dir;
ListFiles(new DirectoryInfo(dir));