C# DirectoryInfo.GetFiles().Where()用法

wjasd123 2016-08-03 10:29:22
现在要实现将某一目录下所有图片格式的文件过滤出来,有*.tif,*.jpg,*.bmp等,GetFiles("*.tif || *.jpg")这样写又不对,我在网上查用where,但是还是不行,麻烦大神帮忙看一下吧
...全文
1081 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjasd123 2016-08-09
  • 打赏
  • 举报
回复
引用 12 楼 yuankaiwsl 的回复:
好说,加个ToLower
string CommonImgPath = "";
List<string> extensions = new List<string>() { ".tif", ".jpg", ".bmp" };
var files = Directory.GetFiles(CommonImgPath);
foreach (var file in files)
{
    string ext = Path.GetExtension(file);
    if (extensions.Contains(ext.ToLower()))
        Console.WriteLine(file);
}
啊,谢谢谢谢,嘿嘿,以前一个个比较太笨了。
wjasd123 2016-08-09
  • 打赏
  • 举报
回复
引用 10 楼 Benjay77 的回复:
[quote=引用 9 楼 wjasd123 的回复:]
[quote=引用 6 楼 Benjay77 的回复:]
[quote=引用 5 楼 xuzuning 的回复:]
.Where(s => s.EndsWith(".tif") || s.EndsWith(".jpg"));
应写作
.Where(s => s.Extension == ".tif" || s.Extension == ".jpg");

这个正解!!!Extension 是扩展名 文件操作类有个方法GETExtensions 获取扩展名[/quote]
请问这个s是什么啊,不需要怎么获取吗[/quote]

不需要 [/quote]


还是不可以啊
巴士上的邂逅 2016-08-09
  • 打赏
  • 举报
回复
好说,加个ToLower
string CommonImgPath = "";
List<string> extensions = new List<string>() { ".tif", ".jpg", ".bmp" };
var files = Directory.GetFiles(CommonImgPath);
foreach (var file in files)
{
    string ext = Path.GetExtension(file);
    if (extensions.Contains(ext.ToLower()))
        Console.WriteLine(file);
}
wjasd123 2016-08-09
  • 打赏
  • 举报
回复
引用 3 楼 yuankaiwsl 的回复:
string CommonImgPath = "";
List<string> extensions = new List<string>() { ".tif", ".jpg", ".bmp" };
var files = Directory.GetFiles(CommonImgPath);
foreach (var file in files)
{
    string ext = Path.GetExtension(file);
    if (extensions.Contains(ext))
        Console.WriteLine(file);
}
这种我试了,这样其实是字符串比较,是区分大小写的,万一我的图片后缀用的大写的形式就搜索不出来了,而我想要的是只要是图片类型都可以,不能图片是A.jpg就可以检测到,A.JPG就不行了。
Benjay77 2016-08-09
  • 打赏
  • 举报
回复
引用 9 楼 wjasd123 的回复:
[quote=引用 6 楼 Benjay77 的回复:] [quote=引用 5 楼 xuzuning 的回复:] .Where(s => s.EndsWith(".tif") || s.EndsWith(".jpg")); 应写作 .Where(s => s.Extension == ".tif" || s.Extension == ".jpg");
这个正解!!!Extension 是扩展名 文件操作类有个方法GETExtensions 获取扩展名[/quote] 请问这个s是什么啊,不需要怎么获取吗[/quote] 不需要
wjasd123 2016-08-09
  • 打赏
  • 举报
回复
引用 6 楼 Benjay77 的回复:
[quote=引用 5 楼 xuzuning 的回复:] .Where(s => s.EndsWith(".tif") || s.EndsWith(".jpg")); 应写作 .Where(s => s.Extension == ".tif" || s.Extension == ".jpg");
这个正解!!!Extension 是扩展名 文件操作类有个方法GETExtensions 获取扩展名[/quote] 请问这个s是什么啊,不需要怎么获取吗
wjasd123 2016-08-09
  • 打赏
  • 举报
回复
引用 7 楼 duxinyulhooq 的回复:
[quote=引用 4 楼 guwei4037 的回复:]
 string directory = @"E:\pic";
            string[] files = Directory.GetFiles(directory).Where(x => x.EndsWith(".tif") || x.EndsWith(".jpg")).ToArray();
经验证,这种方法可行[/quote] 请问这个x到底是什么啊
duxinyulhooq 2016-08-04
  • 打赏
  • 举报
回复
引用 4 楼 guwei4037 的回复:
 string directory = @"E:\pic";
            string[] files = Directory.GetFiles(directory).Where(x => x.EndsWith(".tif") || x.EndsWith(".jpg")).ToArray();
经验证,这种方法可行
xuzuning 2016-08-03
  • 打赏
  • 举报
回复
.Where(s => s.EndsWith(".tif") || s.EndsWith(".jpg")); 应写作 .Where(s => s.Extension == ".tif" || s.Extension == ".jpg");
全栈极简 2016-08-03
  • 打赏
  • 举报
回复
 string directory = @"E:\pic";
            string[] files = Directory.GetFiles(directory).Where(x => x.EndsWith(".tif") || x.EndsWith(".jpg")).ToArray();
巴士上的邂逅 2016-08-03
  • 打赏
  • 举报
回复
string CommonImgPath = "";
List<string> extensions = new List<string>() { ".tif", ".jpg", ".bmp" };
var files = Directory.GetFiles(CommonImgPath);
foreach (var file in files)
{
    string ext = Path.GetExtension(file);
    if (extensions.Contains(ext))
        Console.WriteLine(file);
}
wjasd123 2016-08-03
  • 打赏
  • 举报
回复
引用 1 楼 wjasd123 的回复:

            string CommonImgPath = "D:\\img";
            System.IO.DirectoryInfo ImgDirectory = new System.IO.DirectoryInfo(CommonImgPath);
            //System.IO.FileInfo[] ImgFiles = ImgDirectory.GetFiles("(*.tif|*.jpg)");
            System.IO.FileInfo[] ImgFiles = ImgDirectory.GetFiles().Where(s => s.EndsWith(".tif") || s.EndsWith(".jpg"));
编辑的时候endswith下面显示红色,我想知道会不会是需要引用什么命名控件,因为是日企,提出错误我也看不懂,会日语的领导出差了,周围都是不会日语的小兵
wjasd123 2016-08-03
  • 打赏
  • 举报
回复

            string CommonImgPath = "D:\\img";
            System.IO.DirectoryInfo ImgDirectory = new System.IO.DirectoryInfo(CommonImgPath);
            //System.IO.FileInfo[] ImgFiles = ImgDirectory.GetFiles("(*.tif|*.jpg)");
            System.IO.FileInfo[] ImgFiles = ImgDirectory.GetFiles().Where(s => s.EndsWith(".tif") || s.EndsWith(".jpg"));
Benjay77 2016-08-03
  • 打赏
  • 举报
回复
引用 5 楼 xuzuning 的回复:
.Where(s => s.EndsWith(".tif") || s.EndsWith(".jpg")); 应写作 .Where(s => s.Extension == ".tif" || s.Extension == ".jpg");
这个正解!!!Extension 是扩展名 文件操作类有个方法GETExtensions 获取扩展名

110,534

社区成员

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

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

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