确切的文件扩展名匹配的GetFiles()?

编程入门 行业动态 更新时间:2024-10-25 16:25:34
本文介绍了确切的文件扩展名匹配的GetFiles()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想检索文件扩展名的精确匹配指定字符串列表。

DirectoryInfo的DI =新的DirectoryInfo (someValidPath); 名单,LT; FileInfo的> MYFILES =新的List<&的FileInfo GT;(); 的foreach(FileInfo的网络连接di.GetFiles(* TXT。)) { myFiles.Add(FI); }

我得到扩展文件*。txt的,但我也得到扩展名为 *。txtx ,所以我所编码达得到,其扩展名的文件的开始以的 TXT 。

这是不是我想要的。我需要抓住所有的文件名,做一个正则表达式匹配\\.txt $(我认为),或​​ .EndsWith(TXT)等,要做到这一点?

谢谢!

有点一种解决办法解决方案

,但是可以筛选出准确的其中, extesion方法匹配

的foreach(FileInfo的网络连接di.GetFiles(* TXT)。凡(FI = GT ;的String.Compare(,fi.Extension,StringComparison.OrdinalIgnoreCaseTXT)== 0)) { myFiles.Add(FI); }

请注意,这将使扩展的情况下不区分大小写的匹配。

I'd like to retrieve a list of files whose extensions match a specified string exactly.

DirectoryInfo di = new DirectoryInfo(someValidPath); List<FileInfo> myFiles = new List<FileInfo>(); foreach (FileInfo fi in di.GetFiles("*.txt")) { myFiles.Add(fi); }

I get the files with extension *.txt but I also get files with the extension *.txtx, so what I've coded amounts to getting the files whose extension starts with txt.

This isn't what I want. Do I need to grab all of the filenames and do a regular expression match to "\\.txt$" (I think), or test each filename string with .EndsWith(".txt"), etc., to accomplish this?

Thanks!

解决方案

Somewhat of a workaround, but you can filter out exact matches with the Where extesion method:

foreach (FileInfo fi in di.GetFiles("*.txt") .Where(fi => string.Compare(".txt", fi.Extension, StringComparison.OrdinalIgnoreCase) == 0)) { myFiles.Add(fi); }

Note that this will make a case insensitive matching of the extension.

更多推荐

确切的文件扩展名匹配的GetFiles()?

本文发布于:2023-11-02 17:52:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553026.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:确切   文件扩展名   GetFiles

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!