C# 从目录中获取文件时排除某些文件扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/755166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exclude certain file extensions when getting files from a directory
提问by Graviton
How to excludecertain file type when getting files from a directory?
从目录中获取文件时如何排除某些文件类型?
I tried
我试过
var files = Directory.GetFiles(jobDir);
But it seems that this function can only choose the file types you want to include, not exclude.
不过好像这个功能只能选择你要包含的文件类型,不能排除。
采纳答案by okutane
You should filter these files yourself, you can write something like this:
你应该自己过滤这些文件,你可以这样写:
var files = Directory.GetFiles(jobDir).Where(name => !name.EndsWith(".xml"));
回答by Biri
Afaik there is no way to specify the exclude patterns. You have to do it manually, like:
Afaik 无法指定排除模式。您必须手动执行此操作,例如:
string[] files = Directory.GetFiles(myDir);
foreach(string fileName in files)
{
DoSomething(fileName);
}
回答by oscarkuo
I guess you can use lambda expression
我猜你可以使用 lambda 表达式
var files = Array.FindAll(Directory.GetFiles(jobDir), x => !x.EndWith(".myext"))
回答by Julien Poulin
You could try something like this:
你可以尝试这样的事情:
var allFiles = Directory.GetFiles(@"C:\Path\", "");
var filesToExclude = Directory.GetFiles(@"C:\Path\", "*.txt");
var wantedFiles = allFiles.Except(filesToExclude);
回答by Chethan
string[] filesToDelete = Directory.GetFiles(@"C:\Temp", "*.der");
foreach (string fileName in filesToDelete)
{
File.Delete(fileName);
}
回答by user3071434
This is my version on the answers I read above
这是我在上面阅读的答案的版本
List<FileInfo> fileInfoList = ((DirectoryInfo)new DirectoryInfo(myPath)).GetFiles(fileNameOnly + "*").Where(x => !x.Name.EndsWith(".pdf")).ToList<FileInfo>();
回答by pask23
I know, this a old request, but about me it's always important.
我知道,这是一个古老的要求,但对我来说这总是很重要的。
if you want exlude a list of file extension: (based on https://stackoverflow.com/a/19961761/1970301)
如果您想排除文件扩展名列表:(基于https://stackoverflow.com/a/19961761/1970301)
var exts = new[] { ".mp3", ".jpg" };
public IEnumerable<string> FilterFiles(string path, params string[] exts) {
return
Directory
.GetFiles(path)
.Where(file => !exts.Any(x => file.EndsWith(x, StringComparison.OrdinalIgnoreCase)));
}
回答by mvk
You can try this,
你可以试试这个
var directoryInfo = new DirectoryInfo("C:\YourPath");
var filesInfo = directoryInfo.GetFiles().Where(x => x.Extension != ".pdb");
回答by Woolly
I came across this looking for a method to do this where the exclusion could use the search pattern rules and not just EndWith type logic.
我遇到了这个,正在寻找一种方法来执行此操作,其中排除可以使用搜索模式规则而不仅仅是 EndWith 类型逻辑。
e.g. Search pattern wildcard specifier matches:
例如搜索模式通配符说明符匹配:
- * (asterisk) Zero or more characters in that position.
- ? (question mark) Zero or one character in that position.
- *(星号)该位置的零个或多个字符。
- ? (问号)该位置的零个或一个字符。
This could be used for the above as follows.
这可以用于上述内容,如下所示。
string dir = @"C:\Temp";
var items = Directory.GetFiles(dir, "*.*").Except(Directory.GetFiles(dir, "*.xml"));
Or to exclude items that would otherwise be included.
或者排除本应包括在内的项目。
string dir = @"C:\Temp";
var items = Directory.GetFiles(dir, "*.txt").Except(Directory.GetFiles(dir, "*HOLD*.txt"));
回答by Alberto Esteban
i used that
我用过
Directory.GetFiles(PATH, "*.dll"))
Directory.GetFiles(PATH, "*.dll"))
and the PATH is:
路径是:
public static string _PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
公共静态字符串_PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);