Vb.net 在VB文本框中显示文件夹中所有文本文件的内容

Vb.net 在VB文本框中显示文件夹中所有文本文件的内容,vb.net,textbox,Vb.net,Textbox,我需要在一个文本框中显示同一文件夹中多个文本文件的内容 我现在的代码: Public Class Form1 Dim dMods1 = "DirectPathToFolderWithTextFiles" Dim dMods2 = "DirectPathToFolderWithTextFiles" Dim dMods3 = "DirectPathToFolderWithTextFiles" Dim dMods4 = "DirectPathToFolderWithTe

我需要在一个文本框中显示同一文件夹中多个文本文件的内容

我现在的代码:

Public Class Form1
    Dim dMods1 = "DirectPathToFolderWithTextFiles"
    Dim dMods2 = "DirectPathToFolderWithTextFiles"
    Dim dMods3 = "DirectPathToFolderWithTextFiles"
    Dim dMods4 = "DirectPathToFolderWithTextFiles"

    Dim fileCount1 As Integer = Directory.GetFiles(dMods1).Length
    Dim fileCount2 As Integer = Directory.GetFiles(dMods2).Length
    Dim fileCount3 As Integer = Directory.GetFiles(dMods3).Length
    Dim fileCount4 As Integer = Directory.GetFiles(dMods4).Length

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim nl = System.Environment.NewLine
        Dim Item = dMods1 & "\*.txt"
        Dim Item2 = dMods1 & "\sada2.txt"
        If Not Item Is Nothing Then

            TextBox2.Text = File.ReadAllText(Item)


        End If

    End Sub
End Class

一个简单的“*.txt”,如“Item”,会得到错误“路径中的无效字符”。如何显示文本框中的内容?还是有更简单的方法

对文件夹中的文件使用for-each循环

        For Each FilePath As String In System.IO.Directory.GetFiles("C:\Windows")
            If FilePath.ToLower.EndsWith(".txt") Then 
                TextBox1.text &= System.IO.File.ReadAllText(FilePath) & vbNewLine & vbNewLine
            End If
        Next

对文件夹中的文件使用for each循环

        For Each FilePath As String In System.IO.Directory.GetFiles("C:\Windows")
            If FilePath.ToLower.EndsWith(".txt") Then 
                TextBox1.text &= System.IO.File.ReadAllText(FilePath) & vbNewLine & vbNewLine
            End If
        Next

我会调查的。我不知道每个循环是如何工作的。我给你举了个例子。你也可以用它来获取给定路径中的所有
.txt
文件。@RezaAghaei这不就是显示文件名吗?因为我需要所有的内容files@Thomsen1707迈凯姆的回答是正确的。我刚刚分享了一种从路径获取
.txt
文件的方法。其余部分与myekem的答案相同(没有if语句)+我会调查的。我不知道每个循环是如何工作的。我给你举了个例子。你也可以用它来获取给定路径中的所有
.txt
文件。@RezaAghaei这不就是显示文件名吗?因为我需要所有的内容files@Thomsen1707迈凯姆的回答是正确的。我刚刚分享了一种从路径获取
.txt
文件的方法。其余部分与myekem的答案相同(没有if语句)+1使用
Path.Combine()
创建合格的路径。但是,没有包含
*
的文件或文件夹。您可能正在混合搜索和打开文件使用
Path.Combine()
创建限定路径。但是,没有包含
*
的文件或文件夹。您可能正在混合搜索和打开文件