在VB.NET中;我正在尝试阅读文本文件(例如文件夹中的第5个文本文件); 我的下面的代码读取所有文件并仅记下LAST文件的值:
Imports System
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MekdamFolder As String
MekdamFolder = Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf("\") + 1)
Dim MekdamFile As String
Dim MekdamFiles As String() = Directory.GetFiles(MekdamFolder, "*.txt")
For Each MekdamFile In MekdamFiles
Next
If System.IO.File.Exists(MekdamFile) Then
IO.File.OpenText(MekdamFile)
TextBox1.Text = System.IO.Path.GetFileNameWithoutExtension(MekdamFile)
Else
MessageBox.Show("MekdamFile Missing")
End If
Dim lines() As String = IO.File.ReadAllLines(MekdamFile)
Dim lineArray As New ArrayList(1000)
For zzzz As Integer = 0 To lines.GetUpperBound(0)
lineArray.Add(lines(zzzz))
RichTextBox1.AppendText(lines(zzzz) & vbCrLf)
Next
End Sub
End Class
任何帮助表示感谢。
答案 0 :(得分:1)
first,last,5th - filenamess以任意顺序返回给你。今天,第一个可能是明天第十个。
' this loop does nothing, except leave Mekdam set to the last file
For Each MekdamFile In MekdamFiles
' add code here to Exit For when MekdamFile
' equals something meaningful
Next
' this can currently only edit the last file found due to the above loop.
If System.IO.File.Exists(MekdamFile) Then
IO.File.OpenText(MekdamFile)
TextBox1.Text = System.IO.Path.GetFileNameWithoutExtension(MekdamFile)
Else
....
您最好寻找特定文件或从用户那里获取。