加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

.net – 读取文件夹中的多个文本文件

发布时间:2020-12-17 00:13:55 所属栏目:大数据 来源:网络整理
导读:我在文件夹中有很多文本文件.我现在能做的是一次阅读一个文本并将其插入数据库.我的小应用程序在调试时读取文本文件.因此,我需要多次运行它来读取所有这些文本文件并将它们导入数据库. 我的问题是如何一次读取文件夹中的多个文本文件.这是我的代码工作正常,
我在文件夹中有很多文本文件.我现在能做的是一次阅读一个文本并将其插入数据库.我的小应用程序在调试时读取文本文件.因此,我需要多次运行它来读取所有这些文本文件并将它们导入数据库.

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

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

        Dim filelocation As String
        filelocation = "F:txtfilesch25.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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读