15

我有以下代码。

Dim text As String = IO.File.ReadAllText("C:\Example.xtp")

此代码特定于单个文件,但是我想file.readalltext针对特定目录中的每个文件。

我怎样才能做到这一点?

4

4 回答 4

42

您将需要使用IO.Directory.GetFiles函数。

Dim files() As String = IO.Directory.GetFiles("c:\")

For Each file As String In files
  ' Do work, example
  Dim text As String = IO.File.ReadAllText(file)
Next
于 2013-05-21T13:09:35.983 回答
17
Dim fileEntries As String() = Directory.GetFiles("YourPath", "*.txt")
' Process the list of .txt files found in the directory. '
Dim fileName As String

For Each fileName In fileEntries
    If (System.IO.File.Exists(fileName)) Then
        'Read File and Print Result if its true
        ReadFile(fileName)
    End If
    TransfereFile(fileName, 1)
Next
于 2014-11-14T09:36:08.210 回答
5
System.IO.Directory.GetFiles() 

有帮助

于 2013-05-21T13:10:55.640 回答
2

尝试这个:

Dim text As String = ""
Dim files() As String = IO.Directory.GetFiles(sFolder)

For Each sFile As String In files
    text &= IO.File.ReadAllText(sFile)
Next
于 2013-05-21T13:46:30.240 回答