0

我正在编写一个函数来检查文件夹中的多个文件扩展名,然后返回匹配的文件名,但是我在返回结果时遇到了问题。

该函数“有效”,但显然每次循环时,它都会重新分配Directory.GetFiles()to的结果Files,而不是附加它们。谁能告诉我该怎么做?

如果有人想知道,我这样做是因为,据我所知,没有内置的方法可以使用Directory.GetFiles(). 如果这是错误的,请纠正我。谢谢。

Private Function GetFiles(Path As String, SearchPattern As String)

    Dim SearchPatterns() As String = SearchPattern.Split("|") ' The patterns to search
    Dim Files() As String = Nothing ' The files to return to the user

    For Each Pattern In SearchPatterns

        Files = Directory.GetFiles(Path, Pattern)
        Array.Sort(Files)

    Next

    Return Files

End Function
4

1 回答 1

2

改用List(Of T)类和List(Of T).AddRange方法:

Private Function GetFiles(Path As String, SearchPattern As String)

    Dim SearchPatterns() As String = SearchPattern.Split("|") ' The patterns to search
    Dim Files As New List(Of String)

    For Each Pattern In SearchPatterns
        Files.AddRange(Directory.GetFiles(Path, Pattern))
    Next

    Return Files.OrderBy((Function(f) f)).ToArray()
End Function
于 2013-04-14T17:41:59.717 回答