select files by multiple extension name

本文介绍了一种使用LINQ和正则表达式从指定文件夹及其子文件夹中检索多种图片格式(如jpg、png和gif)的方法。通过一个示例VB.NET代码片段展示了如何实现这一功能,同时指出了在处理具有多个扩展名的文件时可能遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The default selectfile's selectpattern only support on exention name.

For example,  command below can only retrieve jpg image.

If you want both jpg and gif, you need two clause and combine the outputs.  

System.IO.Directory.GetFiles("F:\""*.jpg", IO.SearchOption.AllDirectories)

 


Alternatively, you can use LINQ and regular expression like below, but the code has issue when image has two extension like "Signature.jpg.pfs". Need a bit more works on the regular expression.

If someone knows how to write the regular expression, please let me know. Thanks 

ExpandedBlockStart.gif代码
Private Sub loadFiles() 
        
For Each afile In SelectFilesWithExtenstions("F:\Learning\LoadDistinctFileInCheckListBox\file"".jpg|.png|.gif")
            Debug.Print(afile.name)
        
Next
    
End Sub

    
Private Function SelectFilesWithExtenstions(ByVal folder As StringByVal extensions As String
        
Dim dir As New System.IO.DirectoryInfo(folder)
        
Dim fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories)
        
Dim searchExtenstions As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex((extensions))
        
Dim queryGroupByExt = From afile In fileList _
                                Let matches 
= searchExtenstions.Matches(afile.FullName) _
                                Where (searchExtenstions.Matches(afile.FullName).Count 
> 0) _
                                
Select Name = afile.FullName, _
                                Matches 
= From match As System.Text.RegularExpressions.Match In matches _
                                
Select match.Value
        
Return queryGroupByExt
    
End Function

 

 

 

转载于:https://www.cnblogs.com/yangbin990/archive/2011/02/04/1949118.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值