代码之家  ›  专栏  ›  技术社区  ›  Tola

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

  •  2
  • Tola  · 技术社区  · 17 年前

    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。净值3.5

    4 回复  |  直到 17 年前
        1
  •  9
  •   JeffH    17 年前

    调查 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)
    
        2
  •  1
  •   bendewey    17 年前

    ThreadPool.QueueUserWorkItem 。基本上,您需要读取文件夹中的所有文件,并将每个文件排队进行处理。您必须构造一个方法,该方法可以将每个文件作为自己的子程序单独处理。

        3
  •  1
  •   BobbyShaftoe    17 年前
    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
    
        4
  •  1
  •   Guffa    17 年前

    使用目录。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
    



    "$"C .
    当你在最后一个项目时,不要打破循环,只需让循环提前一个项目结束即可。