VB.NET: Readline from line until end

VB.NET: Readline from line until end

我想问一下有没有办法使用"readline",它从某一行读取到文件末尾。 我需要它,因为用户可以将程序中的值保存到文本文件中。稍后他有可能将这些值调用回主程序(例如,他想编辑这些值)

一切正常,但我有一个多行文本框,如果用户按 ENTER,则在保存后不可能将数据放在正确的位置。 我搜索的是一种告诉程序从第 20 行读取到文件末尾的方法

我的文本保存代码

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
    Dim Saveb As New SaveFileDialog()
    Dim myStreamWriter As System.IO.StreamWriter
    Saveb.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
    Saveb.CheckPathExists = True
    Saveb.CreatePrompt = False
    Saveb.FileName = "b" & (TextBox1.Text)
    Saveb.Title = "Speichern"
    Saveb.ShowDialog(Me)
    Try
        myStreamWriter = System.IO.File.AppendText(Saveb.FileName)
        myStreamWriter.WriteLine(TextBox1.Text)
        myStreamWriter.WriteLine(TextBox13.Text & ".")
        myStreamWriter.WriteLine(TextBox14.Text & ".")
        myStreamWriter.WriteLine(ComboBox5.Text & ".")
        myStreamWriter.WriteLine(ComboBox6.Text & ".")
        myStreamWriter.WriteLine(TextBox4.Text & ".")
        myStreamWriter.WriteLine(SrNr.Text & ".")
        myStreamWriter.WriteLine(ComboBox4.Text & ".")
        myStreamWriter.WriteLine(ComboBox2.Text & ".")
        myStreamWriter.WriteLine(TextBox5.Text & ".")
        myStreamWriter.WriteLine(TextBox6.Text & ".")
        myStreamWriter.WriteLine(TextBox7.Text & ".")
        myStreamWriter.WriteLine(TextBox8.Text & ".")
        myStreamWriter.WriteLine(TextBox9.Text & ".")
        myStreamWriter.WriteLine(TextBox10.Text & ".")
        myStreamWriter.WriteLine(TextBox2.Text & ".")
        myStreamWriter.WriteLine(ComboBox1.Text & ".")
        myStreamWriter.WriteLine(TextBox14.Text & ".")
        myStreamWriter.WriteLine(TextBox11.Text & ".")
        myStreamWriter.WriteLine(ComboBox3.Text)
        myStreamWriter.WriteLine(TextBox12.Text & ".")

        myStreamWriter.Flush()
    Catch ex As Exception
    End Try
End Sub

这是我的阅读代码

 Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
    Static count As Integer
    count = count + 1
    OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    OpenFileDialog1.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
    If (OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
        Dim read As IO.StreamReader
        read = IO.File.OpenText(OpenFileDialog1.FileName)
        TextBox17.Text = read.ReadToEnd()
        largetext.Text = read.ReadToEnd()
        read.Close()
        Dim readLines() As String = IO.File.ReadAllLines(OpenFileDialog1.FileName)
        TextBox1.Text = readLines(0)
        TextBox13.Text = readLines(1)
        TextBox14.Text = readLines(2)
        ComboBox5.Text = readLines(3)
        ComboBox6.Text = readLines(4)
        TextBox4.Text = readLines(5)
        SrNr.Text = readLines(6)
        ComboBox4.Text = readLines(7)
        ComboBox2.Text = readLines(8)
        TextBox5.Text = readLines(9)
        TextBox6.Text = readLines(10)
        TextBox7.Text = readLines(11)
        TextBox8.Text = readLines(12)
        TextBox9.Text = readLines(13)

        TextBox10.Text = readLines(14)
        TextBox2.Text = readLines(15)
        ComboBox1.Text = readLines(16)
        TextBox14.Text = readLines(17)
        TextBox11.Text = readLines(18)
        ComboBox3.Text = readLines(19)
        TextBox12.Text = readLines(20)
    End If
End Sub

代码进入一个隐藏的文本框,从那里进入正确的位置。 我知道它不专业,但我是新手

注意。在您的第一个示例(写作)中,您没有关闭文件。使用 using 块或在 TryFinally 块中添加 myStreamWriter.Close()

您已经读取了文件中的所有行,因此您已经将数据存储在一个数组中,使用一点 LINQ 就可以很容易地对数组进行切片

dim remainingLines = readLines.Skip(20).ToArray()

设置所有第一个文本框和组合后,您应该重新创建 TextBox12

Lines 集合
If (OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
    Dim readLines() As String = IO.File.ReadAllLines(OpenFileDialog1.FileName)
    TextBox1.Text = readLines(0)
    .....
    TextBox12.Lines = readLines.Skip(20).ToArray
    ....

    TextBox17.Text = string.Join(Environment.NewLine, readLines)
    largetext.Text = TextBox17.Text

End If

设置文本框的文本 属性 需要您使用 String.Join 重建单个字符串,如最后几行所示

请注意,对 ReadAllLines 的调用消除了使用 StreamReader 调用任何其他内容的需要,您已经将所有行都存储在内存中并且可以直接使用该 readLines 数组