You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET实现列表框显示指定文件夹文件(不含子文件夹并合并双目录)

解决方案

核心修改思路

  • 把搜索选项从IO.SearchOption.AllDirectories改成IO.SearchOption.TopDirectoryOnly,这样只会读取目标文件夹的直接文件,不会遍历子文件夹。
  • 把读取单个文件夹文件的逻辑复用两次,分别读取两个目标路径的文件,再合并结果后添加到列表框。

修改后的完整代码

' 初始化列表框
HourlyList.SelectionMode = SelectionMode.MultiSimple
HourlyList.Items.Clear()

' 定义要合并的两个文件夹路径
Dim folderPathA As String = "C:\filepath\folderA"
Dim folderPathB As String = "C:\filepath\folderB"
' 定义文件搜索模式
Dim searchPattern As String = 2DArray(Row, Column) & "*"

' 读取第一个文件夹的文件(仅当前目录)
Dim dirA As New System.IO.DirectoryInfo(folderPathA)
Dim filesA As System.IO.FileInfo() = dirA.GetFiles(searchPattern, IO.SearchOption.TopDirectoryOnly)

' 读取第二个文件夹的文件(仅当前目录)
Dim dirB As New System.IO.DirectoryInfo(folderPathB)
Dim filesB As System.IO.FileInfo() = dirB.GetFiles(searchPattern, IO.SearchOption.TopDirectoryOnly)

' 合并两个文件夹的文件集合
Dim allFiles As System.IO.FileInfo() = filesA.Concat(filesB).ToArray()

' 将合并后的文件添加到列表框
For Each file In allFiles
    ' 可根据需求选择添加file(显示完整路径信息)或file.Name(仅显示文件名)
    HourlyList.Items.Add(file.Name)
Next

额外说明

  • 如果需要避免重复文件,可以在合并时加上.Distinct()方法,比如:filesA.Concat(filesB).Distinct().ToArray(),需确保文件的比较逻辑符合你的需求。
  • 若路径是动态获取的,直接替换folderPathA和folderPathB的值即可。

内容的提问来源于stack exchange,提问作者Nate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 14:47:04