我想问一下是否有办法使用“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
代码进入隐藏的文本框,从那里进入正确的位置。 我知道它不专业,但我是新手。
答案 0 :(得分:1)
设置完所有第一个文本框和组合后,您应该重新创建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
设置文本框的Text属性需要使用String.Join重建单个字符串,如最后几行所示
请注意,对ReadAllLines的调用无需使用StreamReader调用任何其他内容,您已将所有行都放在内存中,并且可以直接使用该readLines
数组
答案 1 :(得分:0)
<强> NB 即可。在你的第一个样本(写作)中,你没有关闭文件。使用using
块或在myStreamWriter.Close()
的{{1}}块中添加Finally
。
您已经在读取文件中的所有行,因此您已经将数据放入数组中,使用一点LINQ对数组进行切片很容易
Try