温馨提示×

vb怎么遍历文件夹中的所有文件

vb
小亿
184
2023-12-25 11:17:45
栏目: 编程语言

在VB中,可以使用Directory.GetFiles方法来遍历文件夹中的所有文件。以下是一个示例代码:

Imports System.IO

Module Module1

    Sub Main()
        Dim folderPath As String = "C:\Folder" ' 文件夹路径

        ' 检查文件夹是否存在
        If Directory.Exists(folderPath) Then
            ' 获取文件夹中的所有文件
            Dim files As String() = Directory.GetFiles(folderPath)

            ' 遍历文件
            For Each file As String In files
                Console.WriteLine(file)
            Next
        Else
            Console.WriteLine("文件夹不存在")
        End If

        Console.ReadLine()
    End Sub

End Module

在上面的示例中,我们首先检查文件夹是否存在,然后使用Directory.GetFiles方法获取文件夹中的所有文件,并将它们存储在一个字符串数组中。然后,我们使用For Each循环遍历每个文件并打印文件路径。

0