c# - GetFiles 排除一个字符串

标签 c# getfiles directoryinfo

使用下面的代码,我能够获取名称中包含 string 的所有文件:

DirectoryInfo dirInf = new DirectoryInfo(path);
FileInfo[] fInfArray = dirInf.GetFiles("*string*");

但是如何在 GetFiles 中排除一个字符串。
我不是说结尾像 .txt 我想在文件名中排除 string

例如我有以下文件:

wordwordfile.msg  
wordwordfile.txt  
wodfile.txt

并排除 wod 以获得以下文件:

wordwordfile.msg  
wordwordfile.txt  

最佳答案

var files = new DirectoryInfo("C:\\")
                .EnumerateFiles("*string*")
                .Where(f => !f.Name.Contains("wod"))
                // Optional, convert to array if you want
                .ToArray();

关于c# - GetFiles 排除一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51400222/

相关文章:

C# (.NET) 设计缺陷

c# - 在构造函数中声明的 C# 对象的垃圾收集

.net - 当搜索模式包含 3 个扩展字符时,.NET 的 Directory.GetFiles() 中的奇怪功能

c# - 追加和写入目录的访问权限——FileIOPermission 会产生不好的结果吗?

c# - 如何在 WPF 中使用 MVVM 实现过滤器框

c# - 缓存 LINQ 查询 - IEnumerable.Skip()

c# - 如何刷新 System.IO.DirectoryInfo.GetFiles().Length

c# - 如何输出目录层次结构中的每个文件和文件夹

C#:使用 DirectoryInfo 时如何登录共享

c# - 程序不收集目录信息