VB.NET 从文本文件写入/读取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39770163/
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
VB.NET write/read data from text file
提问by JasonK
This might be something that I am overlooking, as I am currently reading a book on VB.NET. I canceled a course in VB.Net recently because I was stuck on a problem and the lecturer did not get back to me with my questions that I had.
这可能是我忽略的事情,因为我目前正在阅读一本关于 VB.NET 的书。我最近取消了 VB.Net 中的一门课程,因为我遇到了一个问题,而讲师没有回答我的问题。
The objective was to create a business application with VB.NET that writes data from text boxes to a text file, seperated by a comma or a pipe. The user of the application must be able to select the directory where they want the file to be saved.
目标是使用 VB.NET 创建一个业务应用程序,该应用程序将数据从文本框写入文本文件,由逗号或管道分隔。应用程序的用户必须能够选择他们想要保存文件的目录。
So I have for example 2 forms, one that captures the data for a client, and another form where you can select from a drop-down control, Now I know that streamwriterallows for the user to select self where they want the file to be saved, but how do I make the second form intelligent to know where the user saved the form and then reads the client_id, and fills the other data associated with the client_idto the text boxes in the form. I know streamreaderis the one to use when you want to read data from a file, but how will streamreaderknow where the user will save the file to?
所以我有例如 2 个表单,一个为客户端捕获数据,另一个表单,您可以从下拉控件中进行选择,现在我知道流编写器允许用户选择他们想要文件所在的位置已保存,但如何使第二个表单智能地知道用户保存表单的位置,然后读取client_id, 并将与client_id关联的其他数据填充到表单中的文本框。我知道StreamReader的是一个当你想从文件中读取数据时使用,但如何将StreamReader的知道用户将文件保存到?
I am not doing the course any more, but I will keep on thinking what I could have done to actually get the project to work.
我不再做这门课程了,但我会继续思考我可以做些什么才能使项目真正发挥作用。
回答by jarvis
For writing to file
用于写入文件
// Write single line to new file.
Using writer As New StreamWriter("C:\log.txt", True)
writer.WriteLine("Important data line 1")
End Using
For reading from file
用于从文件中读取
// read from a file
Dim line As String
Using reader As New StreamReader("file.txt")
line = reader.ReadLine()
End Using
Console.WriteLine(line)

