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

前端之家收集整理的这篇文章主要介绍了.net – 读取文件夹中的多个文本文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在文件夹中有很多文本文件.我现在能做的是一次阅读一个文本并将其插入数据库.我的小应用程序在调试时读取文本文件.因此,我需要多次运行它来读取所有这些文本文件并将它们导入数据库.

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

  1. Private Sub btnRead_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnRead.Click
  2.  
  3. Dim filelocation As String
  4. filelocation = "F:\txtfiles\ch25.txt"
  5. Dim chid As Integer
  6. chid = 25
  7.  
  8.  
  9.  
  10. 'read from file'
  11. Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath,filelocation))
  12. Dim vArray() As String = MyStream.ReadToEnd.Split(CChar("$"))
  13. MyStream.Close()
  14.  
  15.  
  16.  
  17. Dim count As Integer
  18.  
  19. 'insert text to table'
  20. For d As Integer = 0 To vArray.Length - 1 Step 1
  21.  
  22. If d = vArray.Length - 1 Then
  23. Exit For
  24. End If
  25.  
  26. InsertKh(chid,d + 1,vArray(d))
  27. count = d + 1
  28. Next
  29.  
  30.  
  31. MsgBox ("Done Inserting")
  32.  
  33. End Sub

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

非常感谢.

看看 Directory.GetFiles.

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

  1. Dim fileEntries As String() = Directory.GetFiles(targetDirectory,"*.txt")
  2. ' Process the list of .txt files found in the directory. '
  3. Dim fileName As String
  4. For Each fileName In fileEntries
  5. ProcessFile(fileName)

猜你在找的VB相关文章