具有多个扩展名的GetFiles

可能重复:
你可以调用Directory.GetFiles()与多个filter?

如何过滤多个分机?

我试过了:

FileInfo[] Files = dinfo.GetFiles("*.jpg;*.tiff;*.bmp"); FileInfo[] Files = dinfo.GetFiles("*.jpg,*.tiff,*.bmp"); 

为什么不创build一个扩展方法? 这更可读。

 public static IEnumerable<FileInfo> GetFilesByExtensions(this DirectoryInfo dir, params string[] extensions) { if (extensions == null) throw new ArgumentNullException("extensions"); IEnumerable<FileInfo> files = Enumerable.Empty<FileInfo>(); foreach(string ext in extensions) { files = files.Concat(dir.GetFiles(ext)); } return files; } 

编辑:更有效的版本:

 public static IEnumerable<FileInfo> GetFilesByExtensions(this DirectoryInfo dir, params string[] extensions) { if (extensions == null) throw new ArgumentNullException("extensions"); IEnumerable<FileInfo> files = dir.EnumerateFiles(); return files.Where(f => extensions.Contains(f.Extension)); } 

用法:

 dInfo.GetFilesByExtensions(".jpg",".exe",".gif"); 

你可以得到每个文件,然后过滤数组:

 public static IEnumerable<FileInfo> GetFilesByExtensions(this DirectoryInfo dirInfo, params string[] extensions) { var allowedExtensions = new HashSet<string>(extensions, StringComparer.OrdinalIgnoreCase); return dirInfo.EnumerateFiles() .Where(f => allowedExtensions.Contains(f.Extension)); } 

这将比这里的其他所有答案快(稍微快一点)。
在.net 3.5,用GetFilesreplaceEnumerateFiles (这是较慢)。

你不能这样做,因为GetFiles只接受一个search模式。 相反,您可以调用没有模式的GetFiles ,并在代码中筛选结果:

 string[] extensions = new[] { ".jpg", ".tiff", ".bmp" }; FileInfo[] files = dinfo.GetFiles() .Where(f => extensions.Contains(f.Extension.ToLower())) .ToArray(); 

如果您正在使用.NET 4,则可以使用EnumerateFiles方法来避免一次加载内存中的所有FileInfo对象:

 string[] extensions = new[] { ".jpg", ".tiff", ".bmp" }; FileInfo[] files = dinfo.EnumerateFiles() .Where(f => extensions.Contains(f.Extension.ToLower())) .ToArray(); 

你可以使用LINQ Union方法:

 dir.GetFiles("*.txt").Union(dir.GetFiles("*.jpg")).ToArray(); 

以下检索jpg,tiff和bmp文件,并为您提供一个可以迭代的IEnumerable<FileInfo>

 var files = dinfo.GetFiles("*.jpg") .Concat(dinfo.GetFiles("*.tiff")) .Concat(dinfo.GetFiles("*.bmp")); 

如果你真的需要一个数组,只需在这个结尾处粘贴.ToArray()

我不确定这是否可能。 MSDN GetFiles 引用说明search模式,而不是search模式列表。

我可能会倾向于分别提取每个列表,并将其“foreach”到最终列表中。

我知道有一个更优雅的方式来做到这一点,我愿意提出build议…这就是我所做的:

  try { // Set directory for list to be made of DirectoryInfo jpegInfo = new DirectoryInfo(destinationFolder); DirectoryInfo jpgInfo = new DirectoryInfo(destinationFolder); DirectoryInfo gifInfo = new DirectoryInfo(destinationFolder); DirectoryInfo tiffInfo = new DirectoryInfo(destinationFolder); DirectoryInfo bmpInfo = new DirectoryInfo(destinationFolder); // Set file type FileInfo[] Jpegs = jpegInfo.GetFiles("*.jpeg"); FileInfo[] Jpgs = jpegInfo.GetFiles("*.jpg"); FileInfo[] Gifs = gifInfo.GetFiles("*.gif"); FileInfo[] Tiffs = gifInfo.GetFiles("*.tiff"); FileInfo[] Bmps = gifInfo.GetFiles("*.bmp"); // listBox1.Items.Add(@""); // Hack for the first list item no preview problem // Iterate through each file, displaying only the name inside the listbox... foreach (FileInfo file in Jpegs) { listBox1.Items.Add(file.Name); Photo curPhoto = new Photo(); curPhoto.PhotoLocation = file.FullName; metaData.AddPhoto(curPhoto); } foreach (FileInfo file in Jpgs) { listBox1.Items.Add(file.Name); Photo curPhoto = new Photo(); curPhoto.PhotoLocation = file.FullName; metaData.AddPhoto(curPhoto); } foreach (FileInfo file in Gifs) { listBox1.Items.Add(file.Name); Photo curPhoto = new Photo(); curPhoto.PhotoLocation = file.FullName; metaData.AddPhoto(curPhoto); } foreach (FileInfo file in Tiffs) { listBox1.Items.Add(file.Name); Photo curPhoto = new Photo(); curPhoto.PhotoLocation = file.FullName; metaData.AddPhoto(curPhoto); } foreach (FileInfo file in Bmps) { listBox1.Items.Add(file.Name); Photo curPhoto = new Photo(); curPhoto.PhotoLocation = file.FullName; metaData.AddPhoto(curPhoto); } 

我正在使用以下方式列出文件。

 '////cblSourceFiles is CHECK BOX LIST CONTROL <_asp:CheckBoxList ID="cblSourceFiles" runat="server" DataTextField="Name" DataValueField="Name" /> cblSourceFiles.DataSource = (New IO.DirectoryInfo(Path)).EnumerateFiles().Where(AddressOf ValidExtensions) cblSourceFiles.DataBind() Private Function ValidExtensions(ByVal pFi As System.IO.FileInfo, ByVal pSt As Integer) As Boolean Dim mRet As Boolean = False For Each mExt As String In ".jpg,.png".Split(",") '//// WebConfig.AllowedFiletypes If pFi.Extension.ToLower = mExt.ToLower Then mRet = True Exit For End If Next Return mRet End Function