我需要在文本框的同一文件夹中显示多个文本文件的内容。
我的代码现在:
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”中一样,会得到错误“路径中的无效字符”。如何在文本框中显示内容?或者有更简单的方法吗?
答案 0 :(得分:2)
对文件夹
中文件的每个循环使用a 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