首页 > 解决方案 > 使用 GetFiles() 时如何排除某些文件

问题描述

我有一个包含 2 个子文件夹的文件夹,每个子文件夹包含大约 1000 个文件。文件如下所示:

38485303_SARA_N211_T.ygx
38485303_SARA_N211_B.ygx
38208001_ULTI_CARTRI.ygx

我想要 3 个单独的数组 - 我已经拥有了 2 个。

 Array 1 = all files ending with "_T."
 Array 2 = all files ending with "_B."
 Array 3 = all files that dont end with _T or _B

这就是我对数组 1 和 2 所拥有的。

files = Directory.GetFiles(folderPath, "*_T.*", SearchOption.AllDirectories);
files = Directory.GetFiles(folderPath, "*_B.*", SearchOption.AllDirectories);

这就是我试图形成数组 3 的结果,但没有成功:

files = Directory.GetFiles(folderPath, ".ygx", SearchOption.AllDirectories).Where(file => !file.EndsWith("_T.ygx") || !file.EndsWith("_B.ygx")).ToArray();

标签: c#linqgetfiles

解决方案


我认为这可行。

files = Directory.GetFiles(folderPath, ".ygx", SearchOption.AllDirectories)
.Where(file => !file.EndsWith("_T.ygx") && !file.EndsWith("_B.ygx")).ToArray();

现在您确保文件不以_T.ygx AND ALSO _B.ygx


推荐阅读