vb.net 从多行文本框写入 .txt 文件,然后读回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18421939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing from multiline text box to .txt file, and then reading it back
提问by Schpenn
I have a form with several text boxes and I want to write the contents of each of them to a new line in a .txt file. As in, the user fills in a form, and the info is stored in the file. Then I want to be able to retrieve the info from the file into the same text boxes. I am able to do this, so far, but I encounter problems when one of the text boxes is multiline.
我有一个包含多个文本框的表单,我想将每个文本框的内容写入 .txt 文件中的新行。如在,用户填写表格,信息存储在文件中。然后我希望能够将文件中的信息检索到相同的文本框中。到目前为止,我能够做到这一点,但是当其中一个文本框是多行时,我会遇到问题。
Printline(1, txtBox1.text)
Printline(1, txtBox2.text)′which is the multiline one
Printline(1, txtBox3.text)
When I read this back from the file I get the second line of the multiline text box where I want the text from txtBox3 to be.
当我从文件中读回这个时,我得到了多行文本框的第二行,我希望 txtBox3 中的文本在那里。
LineInput(1, txtBox1.text)
LineInput(1, txtBox2.text)
LineInput(1, txtBox3.text)
How can I get all the lines from the multiline text box to write to one line in the file, and then read it back as separate lines in a multiline text box?
如何从多行文本框中获取所有行以写入文件中的一行,然后在多行文本框中将其作为单独的行读回?
I hope I am making sense? I really would like to keep the logic of "one txtBox - one line in the file"
我希望我说的有道理?我真的很想保留“一个 txtBox - 文件中的一行”的逻辑
I guess I need to use different methods of writing and reading, but I am not that familiar with this, so any help is much appreciated.
我想我需要使用不同的写作和阅读方法,但我对此并不熟悉,因此非常感谢任何帮助。
采纳答案by varocarbas
You can rely on the LinesProperty in case of having more than one line. Sample code (curTextBoxis the given TextBox Control):
如果Lines有多条线路,您可以依赖该物业。示例代码(curTextBox是给定的TextBox Control):
Using writer As System.IO.StreamWriter = New System.IO.StreamWriter("path", True)
Dim curLine As String = curTextBox.Text
If (curTextBox.Lines.Count > 1) Then
curLine = ""
For Each line As String In curTextBox.Lines
curLine = curLine & " " & line
Next
curLine = curLine.Trim()
End If
writer.WriteLine(curLine)
End Using
NOTE: this code puts in one line all the text from the given TextBoxindependently upon its number of lines. If it has more than one line, it includes a blank space to separate the individual lines (all of them fitting in a single line of the file anyway). You might want to change this last feature by adding a different separating character (replace & " " &with the one you want).
注意:此代码将给定的所有文本TextBox独立于其行数放在一行中。如果它有多于一行,它会包含一个空格来分隔各个行(无论如何,它们都适合文件的一行)。您可能希望通过添加不同的分隔字符(替换& " " &为您想要的字符)来更改最后一个功能。
回答by andyg0808
One option would be to escapethe newlines so that they aren't in the output, then unescape them on reading back in.
一种选择是转义换行符,使它们不在输出中,然后在读回时取消转义它们。
Here's some example code that will do this (I've never written VB before, so this probably isn't idiomatic):
这是一些可以执行此操作的示例代码(我以前从未编写过 VB,所以这可能不是惯用的):
' To output to a file:
Dim output As String = TextBox2.Text
' Escape all the backslashes and then the vbCrLfs
output = output.Replace("\", "\bk").Replace(vbCrLf, "\crlf")
' Write the data from output to the file
' To read data from the file:
Dim input As String = ' Put the data from the file in input
' Put vbCrLfs back for \crlf, then put \ for \bk
input = input.Replace("\crlf", vbCrLf).Replace("\bk", "\")
' Put the text back in its box
TextBox2.Text = input
Another option would be to store your data in XML, JSON, or YAML. Any of those are text-based formats that will require a library to parse, but should cleanly handle the multiline text you have, along with providing increased future flexibility.
另一种选择是将数据存储在XML、JSON或YAML 中。其中任何一种都是基于文本的格式,需要一个库来解析,但应该干净地处理您拥有的多行文本,并提供更高的未来灵活性。

