从目录中获取文件时如何排除某些文件类型?
我试过了
var files = Directory.GetFiles(jobDir);
Run Code Online (Sandbox Code Playgroud)
但似乎此功能只能选择要包含的文件类型,而不能排除.
oku*_*ane 85
你应该自己过滤这些文件,你可以写这样的东西:
var files = Directory.GetFiles(jobDir).Where(name => !name.EndsWith(".xml"));
Run Code Online (Sandbox Code Playgroud)
Jul*_*lin 13
你可以尝试这样的事情:
var allFiles = Directory.GetFiles(@"C:\Path\", "");
var filesToExclude = Directory.GetFiles(@"C:\Path\", "*.txt");
var wantedFiles = allFiles.Except(filesToExclude);
Run Code Online (Sandbox Code Playgroud)
pas*_*k23 13
我知道,这是一个古老的要求,但关于我,这一直是重要的.
如果你想要排除文件扩展名列表:(基于/sf/answers/1397323301/)
var exts = new[] { ".mp3", ".jpg" };
public IEnumerable<string> FilterFiles(string path, params string[] exts) {
return
Directory
.GetFiles(path, SearchOption.AllDirectories)
.Where(file => !exts.Any(x => file.Extension.EndsWith(x, StringComparison.OrdinalIgnoreCase)));
}
Run Code Online (Sandbox Code Playgroud)
osc*_*kuo 10
我想你可以使用lambda表达式
var files = Array.FindAll(Directory.GetFiles(jobDir), x => !x.EndWith(".myext"))
Run Code Online (Sandbox Code Playgroud)