C#如何统计当前目录中的文件个数

nauky_911 2008-01-13 07:44:33
想统计某指定路径下的word文件的个数
...全文
1182 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
redstone8415 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

我只想问一句
上面使用GetFiles方法的
有没有试过
如果文件夹里有10万个文件
GetFiles会不会卡死呢?
[/Quote]

是呀! 我同意!
SocketUpEx 2012-07-02
  • 打赏
  • 举报
回复
我只想问一句
上面使用GetFiles方法的
有没有试过
如果文件夹里有10万个文件
GetFiles会不会卡死呢?
zhoulinli915 2012-07-02
  • 打赏
  • 举报
回复
string [] fileEntries = Directory.GetFiles(targetDirectory);然后获取数组长度,就要所有的文件的个数
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

我只想问一句
上面使用GetFiles方法的
有没有试过
如果文件夹里有10万个文件
GetFiles会不会卡死呢?
[/Quote]
理论上这个不是遍历求和的方法,只是在文件分配表上获取的,实际没测试过,但是卡死还没遇到过。
bbqqqbq 2009-02-16
  • 打赏
  • 举报
回复
public static int GetFilesCount(string pmFilePath)
{
int totalFile = 0;
totalFile = Directory.GetFiles(pmFilePath,"*.doc").Length;
string[] fileList = Directory.GetDirectories(pmFilePath);
foreach (string fileChild in fileList)
{
totalFile += GetFilesCount(fileChild);
}
return totalFile;
}
xieshy 2008-08-04
  • 打赏
  • 举报
回复
System.IO.Directory.GetFiles(path,"*.doc").Length
卧_槽 2008-08-04
  • 打赏
  • 举报
回复
System.IO.Directory.GetSysFiles(path,"*.doc").Length
hejialin666 2008-08-04
  • 打赏
  • 举报
回复
如果文件夹中还有文件夹怎么办?
还有,如果不知道文件夹中是什么类型的文件,想获取文件个数怎么办?
谢谢了!
nauky_911 2008-01-14
  • 打赏
  • 举报
回复
恩,谢谢
春天的气息 2008-01-13
  • 打赏
  • 举报
回复
2楼正解,
System.IO.Directory.GetFiles(path,"*.doc").Length

用 foreach其实也想知道doc文件的数量,直接获取数组大小就最直观了。
ProjectDD 2008-01-13
  • 打赏
  • 举报
回复
up
duping9626 2008-01-13
  • 打赏
  • 举报
回复
System.I.Directory.GetFiles(path,"*.doc").Length
楼外楼 2008-01-13
  • 打赏
  • 举报
回复

int FileCount = 0;
DirectoryInfo Dir = new DirectoryInfo("你的PATH");
foreach (FileInfo FI in Dir.GetFiles())
{
if(System.IO.Path.GetExtension(FI.Name) == ".doc")
{
FileCount++;
}
}

110,538

社区成员

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

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

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