求一个好点的方法判断一个文件夹下是否存在文件

fscnt 2010-05-14 03:20:59

一个文件夹下有许多子文件夹,子文件夹下还有许多子文件夹

怎么判断这个文件夹有文件!
...全文
192 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
horfy311 2011-08-08
  • 打赏
  • 举报
回复
没有找到我想要的……
noway8881 2010-05-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wuyq11 的回复:]

foreach (string dir in System.IO.Directory.GetDirectories("", "*.*", SearchOption.AllDirectories))
{
System.IO.DirectoryInfo di = new DirectoryInfo(dir);
……
[/Quote]

人家都说有很多文件夹了。这么写不好。
应该找到文件立即返回true。递归。
wanghuaide 2010-05-14
  • 打赏
  • 举报
回复
学习了
wangwenzhuang 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhuazhuqingchong 的回复:]
System.IO.Directory.GetFiles()
[/Quote]
这个可以获取目录下是否有子文件
wuyq11 2010-05-14
  • 打赏
  • 举报
回复
foreach (string dir in System.IO.Directory.GetDirectories("", "*.*", SearchOption.AllDirectories))
{
System.IO.DirectoryInfo di = new DirectoryInfo(dir);
if (di.GetFiles().Length == 0)
Console.WriteLine(dir);
}

zzx509 2010-05-14
  • 打赏
  • 举报
回复
2楼的就可以了。简洁明了。
waitdream 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mervyn807 的回复:]
引用 3 楼 zhuazhuqingchong 的回复:
System.IO.Directory.GetFiles()

就这个吧
[/Quote]

这个不包括子目录吧?
1L正解,递归获取~
waiwai1015 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mervyn807 的回复:]
引用 3 楼 zhuazhuqingchong 的回复:
System.IO.Directory.GetFiles()

就这个吧
[/Quote]
支持
缭绕飘渺 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhuazhuqingchong 的回复:]
System.IO.Directory.GetFiles()
[/Quote]
就这个吧
zhuazhuqingchong 2010-05-14
  • 打赏
  • 举报
回复
System.IO.Directory.GetFiles()
mayonglong 2010-05-14
  • 打赏
  • 举报
回复
System.IO.Directory.GetFiles(path,"*.*",SearchOption.AllDirectories)
deknight 2010-05-14
  • 打赏
  • 举报
回复

public static long GetFilesSize(String path){
DirectoryInfo directoryInfo=new DirectoryInfo(path);
long length=0;
foreach( FileSystemInfo fsi in directoryInfo.GetFileSystemInfos() ) {
if ( fsi is FileInfo ) {
length += ((FileInfo)fsi).Length;
}
else {
length +=GetFilesSize(fsi.FullName);
}
}
return length;
}

大于0就是有文件喽

110,533

社区成员

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

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

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