读取文件夹中的多个文本文件

Ang*_*Wat 2 .net vb.net

我在文件夹中有很多文本文件.我现在可以做的是一次阅读一个文本并将其插入数据库.我调试时,我的小应用程序读取文本文件.因此,我需要多次运行它来读取所有这些文本文件并将它们导入数据库.

我的问题是如何一次读取文件夹中的多个文本文件.这是我的代码工作正常,但它一次只能读取一个文本文件.

Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click

        Dim filelocation As String
        filelocation = "F:\txtfiles\ch25.txt"
        Dim chid As Integer
        chid = 25



        'read from file'
        Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
        Dim vArray() As String = MyStream.ReadToEnd.Split(CChar("$"))
        MyStream.Close()



        Dim count As Integer

        'insert text to table'
        For d As Integer = 0 To vArray.Length - 1 Step 1

            If d = vArray.Length - 1 Then
                Exit For
            End If

            InsertKh(chid, d + 1, vArray(d))
            count = d + 1
        Next


       MsgBox ("Done Inserting")

End Sub
Run Code Online (Sandbox Code Playgroud)

显然,我需要一种方法来遍历文件夹并检查是否有文本文件.但我无法做到对.谁能给我看一些代码或链接?我正在使用VB.NET,.NET 3.5

非常感谢.

Jef*_*ffH 9

查看Directory.GetFiles.

您可以使用指定的搜索模式(如"*.txt")调用它来查找特定类型的文件.像这样的东西:

    Dim fileEntries As String() = Directory.GetFiles(targetDirectory,"*.txt")
    ' Process the list of .txt files found in the directory. '
    Dim fileName As String
    For Each fileName In fileEntries
        ProcessFile(fileName)
Run Code Online (Sandbox Code Playgroud)