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

如何将多个FileSystemInfo数组合并为单一FileSystemInfo集合?

合并多个目录的FileSystemInfo集合(VB.NET)

嗨,我来帮你搞定这个问题!你现在有五个分别从不同目录获取的FileSystemInfo数组,想要合并成一个单一集合对吧?这里有两种靠谱的方法,看你需求选:

方法1:合并已有的五个FileSystemInfo数组

如果你已经生成了FSinfo1到FSinfo5这五个数组,最简单的方式是用LINQ的Concat方法把它们依次连接起来,最后转成数组或者List都可以。记得要先导入System.Linq命名空间哦。

用LINQ Concat实现

' 先确保导入LINQ命名空间
Imports System.Linq

' 合并五个数组并转成FileSystemInfo数组
Dim SINGLE_FSinfo_ALL As FileSystemInfo() = FSinfo1.Concat(FSinfo2).Concat(FSinfo3).Concat(FSinfo4).Concat(FSinfo5).ToArray()

' 或者转成List(Of FileSystemInfo)
Dim SINGLE_FSinfo_List As List(Of FileSystemInfo) = FSinfo1.Concat(FSinfo2).Concat(FSinfo3).Concat(FSinfo4).Concat(FSinfo5).ToList()

用List.AddRange实现(无需LINQ)

如果你不想用LINQ,也可以用List(Of FileSystemInfo)的AddRange方法逐个添加数组,逻辑更直观:

' 初始化空集合
Dim combinedList As New List(Of FileSystemInfo)()

' 逐个添加已有数组
combinedList.AddRange(FSinfo1)
combinedList.AddRange(FSinfo2)
combinedList.AddRange(FSinfo3)
combinedList.AddRange(FSinfo4)
combinedList.AddRange(FSinfo5)

' 转成数组(按需选择)
Dim SINGLE_FSinfo_ALL As FileSystemInfo() = combinedList.ToArray()

方法2:直接从五个目录获取单一集合

其实不用先单独生成五个数组,我们可以一步到位:先把所有目录路径整理成一个集合,然后遍历每个目录获取FileSystemInfo并合并,这样代码更简洁,还能减少中间变量。

循环遍历写法

' 把五个目录路径整理到一个数组里
Dim dirPaths As String() = {str_fromPath1, str_fromPath2, str_fromPath3, str_fromPath4, str_fromPath5}

' 初始化空集合
Dim allFSInfo As New List(Of FileSystemInfo)()

' 遍历每个目录,添加其FileSystemInfo到集合
For Each path In dirPaths
    Dim dir As New DirectoryInfo(path)
    allFSInfo.AddRange(dir.GetFileSystemInfos())
Next

' 转成数组(按需选择)
Dim SINGLE_FSinfo_ALL As FileSystemInfo() = allFSInfo.ToArray()

LINQ简洁写法

如果喜欢更紧凑的代码,用LINQ的SelectMany可以实现“平铺”多个数组为单一序列,非常适合这种场景:

Imports System.Linq

' 整理目录路径
Dim dirPaths As String() = {str_fromPath1, str_fromPath2, str_fromPath3, str_fromPath4, str_fromPath5}

' 一步获取合并后的数组
Dim SINGLE_FSinfo_ALL As FileSystemInfo() = dirPaths.SelectMany(Function(path) New DirectoryInfo(path).GetFileSystemInfos()).ToArray()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 12:12:34