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

时间:2009-03-20 21:25:46

标签: .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

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

非常感谢。

4 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

我会考虑使用ThreadPool.QueueUserWorkItem。基本上,您需要读取文件夹中的所有文件并将每个文件排入队列进行处理。您必须构建一个方法,可以将每个文件作为自己的子例程单独处理。

答案 2 :(得分:1)

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

Dim di As New DirectoryInfo("F:\txtfiles")
Dim s As String

ForEach s In di.GetFiles("*.txt")
    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        Dim filelocation As String
        filelocation = s ''"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

答案 3 :(得分:1)

使用Directory.GetFiles方法查找文件夹中的所有文本文件:

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

   Dim files As String() = System.IO.Directory("F:\txtfiles\", "*.txt")

   Dim count As Integer = 0

   ForEach filelocation As String in files

      Dim chid As Integer = 25

      'read from file'
      Dim MyStream As New StreamReader(filelocation)
      Dim vArray() As String = MyStream.ReadToEnd.Split("$"C)
      MyStream.Close()

      'insert text to table'
      For d As Integer = 0 To vArray.Length - 2
         InsertKh(chid, d + 1, vArray(d))
         count = count + 1
      Next

   Next

   MsgBox ("Done Inserting")

End Sub

注意:
当第二个参数是完整路径时使用Path.Combine只返回第二个参数,因此在这种情况下它没用。
您可以编写如下字符文字来代替将字符串转换为字符:"$"C 当你在最后一个项目时,不要让循环结束,只需使循环结束一个项目 由于你有嵌套循环,你必须在循环中增加计数器而不是分配循环变量,否则你只会得到最后一个文件中的项目数。 (如果您实际上打算使用计数器。)

相关问题