.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

非常感谢.

看看 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)

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...