我们可以使用多个过滤器调用Directory.GetFiles()吗?
时间:2020-03-06 15:02:06 来源:igfitidea点击:
我正在尝试使用Directory.GetFiles()方法来检索多种类型的文件列表,例如mp3和jpg。我没有运气就尝试了以下两种方法:
Directory.GetFiles("C:\path", "*.mp3|*.jpg", SearchOption.AllDirectories); Directory.GetFiles("C:\path", "*.mp3;*.jpg", SearchOption.AllDirectories);
有没有办法在一个电话中做到这一点?
解决方案
没有。请尝试以下方法:
List<string> _searchPatternList = new List<string>(); ... List<string> fileList = new List<string>(); foreach ( string ext in _searchPatternList ) { foreach ( string subFile in Directory.GetFiles( folderName, ext ) { fileList.Add( subFile ); } } // Sort alpabetically fileList.Sort(); // Add files to the file browser control foreach ( string fileName in fileList ) { ...; }
摘自:http://blogs.msdn.com/markda/archive/2006/04/20/580075.aspx
是的...我相信我们必须拨打任意数量的所需文件类型。
我将自己创建一个函数,在具有所需扩展名的字符串上获取数组,然后在该数组上进行迭代以进行所有必要的调用。该函数将返回与我发送的扩展名匹配的文件的一般列表。
希望能帮助到你。
对于.NET 4.0及更高版本,
var files = Directory.EnumerateFiles("C:\path", "*.*", SearchOption.AllDirectories) .Where(s => s.EndsWith(".mp3") || s.EndsWith(".jpg"));
对于早期版本的.NET,
var files = Directory.GetFiles("C:\path", "*.*", SearchOption.AllDirectories) .Where(s => s.EndsWith(".mp3") || s.EndsWith(".jpg"));
编辑:请阅读评论。 Paul Farry建议的改进以及Christian.K指出的内存/性能问题都很重要。
刚刚找到了另一种方法来做到这一点。仍然不是一项手术,而是将其抛出来看看其他人对此有何看法。
private void getFiles(string path) { foreach (string s in Array.FindAll(Directory.GetFiles(path, "*", SearchOption.AllDirectories), predicate_FileMatch)) { Debug.Print(s); } } private bool predicate_FileMatch(string fileName) { if (fileName.EndsWith(".mp3")) return true; if (fileName.EndsWith(".jpg")) return true; return false; }
以下函数搜索多个模式,并用逗号分隔。我们也可以指定排除,例如:"!web.config"将搜索所有文件,而排除" web.config"。模式可以混合。
private string[] FindFiles(string directory, string filters, SearchOption searchOption) { if (!Directory.Exists(directory)) return new string[] { }; var include = (from filter in filters.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) where !string.IsNullOrEmpty(filter.Trim()) select filter.Trim()); var exclude = (from filter in include where filter.Contains(@"!") select filter); include = include.Except(exclude); if (include.Count() == 0) include = new string[] { "*" }; var rxfilters = from filter in exclude select string.Format("^{0}$", filter.Replace("!", "").Replace(".", @"\.").Replace("*", ".*").Replace("?", ".")); Regex regex = new Regex(string.Join("|", rxfilters.ToArray())); List<Thread> workers = new List<Thread>(); List<string> files = new List<string>(); foreach (string filter in include) { Thread worker = new Thread( new ThreadStart( delegate { string[] allfiles = Directory.GetFiles(directory, filter, searchOption); if (exclude.Count() > 0) { lock (files) files.AddRange(allfiles.Where(p => !regex.Match(p).Success)); } else { lock (files) files.AddRange(allfiles); } } )); workers.Add(worker); worker.Start(); } foreach (Thread worker in workers) { worker.Join(); } return files.ToArray(); }
用法:
foreach (string file in FindFiles(@"D:8.2.11", @"!*.config, !*.js", SearchOption.AllDirectories)) { Console.WriteLine(file); }
让
var set = new HashSet<string> { ".mp3", ".jpg" };
然后
Directory.GetFiles(path, "*.*", SearchOption.AllDirectories) .Where(f => set.Contains( new FileInfo(f).Extension, StringComparer.OrdinalIgnoreCase));
或者
from file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories) from ext in set where String.Equals(ext, new FileInfo(file).Extension, StringComparison.OrdinalIgnoreCase) select file;
List<string> FileList = new List<string>(); DirectoryInfo di = new DirectoryInfo("C:\DirName"); IEnumerable<FileInfo> fileList = di.GetFiles("*.*"); //Create the query IEnumerable<FileInfo> fileQuery = from file in fileList where (file.Extension.ToLower() == ".jpg" || file.Extension.ToLower() == ".png") orderby file.LastWriteTime select file; foreach (System.IO.FileInfo fi in fileQuery) { fi.Attributes = FileAttributes.Normal; FileList.Add(fi.FullName); }