我有一个文件夹中的许多文本文件。我现在能做的就是通读一个文本的时间,并将其插入到数据库中。我的小应用程序读取,当我调试它的文本文件。所以,我需要跑几次才能阅读所有的文本文件,并将其导入到数据库中。
I have many text files in a folder. What I can do now is to read through one text at a time and insert it into the database. My little app reads a text file when I debug it. So, I need to run it several times to read all those text files and import them into the database.
我的问题是如何读取多个文本文件的文件夹内的时间。这是我的code,工作正常,但在同一时间只能读取一个文本文件。
My question is how to read multiple text files inside a folder at a time. Here's my code which works fine but it reads only one text files at a time.
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
很显然,我需要一种方法来通过一个文件夹循环,并检查是否有文本文件。但我不能得到它的权利。任何人都可以给我一些code或链接?我使用VB.NET,.NET 3.5
Obviously, I need a way to loop through a folder and check if there's text file. But I cant get it right. Can anyone show me some code or links ? I'm using VB.NET, .NET 3.5
太感谢了。
。看看Directory.GetFiles.
您可以用类似的* .txt指定的搜索模式调用它来查找特定类型的文件。事情是这样的:
You can call it with a specified search pattern like "*.txt" to find a specific kind of file. Something like this:
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)