请问如何用c#遍历一个文件夹下的所有文件包括子文件夹?

bkm2 2007-05-06 06:30:50
请问如何用c#遍历一个文件夹下的所有文件包括子文件夹?
...全文
529 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
amandag 2007-05-06
  • 打赏
  • 举报
回复
using System;
using System.IO;

class ListAllFilesDemo
{
public static void Main()
{
Console.Write("请输入要查询的目录: ");
string dir = Console.ReadLine();
try
{
ListFiles(new DirectoryInfo(dir));
}
catch(IOException e)
{
Console.WriteLine(e.Message);
}
}

public static 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);
//对于子目录,进行递归调用
else
ListFiles(files[i]);

}
}
}
  • 打赏
  • 举报
回复
http://www.planev.net/article.asp?id=4

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧