VB.Net - 从文本框写入文本文件
嘿伙计们,这里还有一个小问题!尝试为大学作品集编写测验,但在写入 .txt 文本文件时遇到问题。在一种表单(form4.vb)上,我有一个列表框,用于获取名为“用户名”的记事本文本文件中保存的信息,其中包含测验用户的姓名。当手动写入此文本文件时,我的列表框可以很好地接收它,但是,在不同的表单(form3.vb)上,我有一个文本框,用户在其中输入他们的名字,这应该转到“usernames.txt”文本文件由另一种表单上的列表框选取,但它根本不写入任何内容,如果该文本文件上已经有文本,则会将其全部清除。 我还必须使用 application.startup 路径而不是通常的 C:\my documentents\ 等,所以我必须从这样的东西开始:(注意:由于混淆了不同的代码,代码有点混乱变化,但这只是一个例子)
'Try
' Dim appPath As String
' Dim fileName As String
' appPath = Application.StartupPath
' fileName = appPath & "\usernames.txt"
' sWriter = New System.IO.StreamWriter(fileName)
' sWriter.Close()
' MessageBox.Show("Writing file to disk")
'Catch ex As Exception
' MessageBox.Show("File Access Error", "Error")
'End Try
'MessageBox.Show("Program terminating")
'Application.Exit()
希望有人可以提供帮助! =)
Hey guys, just another little problem here! Trying to write a quiz for a college portfolio and having trouble with writing to a .txt textfile. On one form(form4.vb), I have a listbox that picks up the information held within a notepad textfile called "usernames" which contains names of quiz users. When written in manually to this textfile, my listbox picks it up fine, however, on a different form(form3.vb), I have a textbox where a user inputs their name, this is supposed to go to the "usernames.txt" textfile to be picked up by the listbox on the other form but instead, it does not write anything at all and if there is already text on this textfile, it wipes it all out.
I also have to use the application.startup path instead of the usual C:\my documentents\ etc so i would have to begin with something like this: (Note: code is a little mixed up due to messing around with different variations but this is just a example)
'Try
' Dim appPath As String
' Dim fileName As String
' appPath = Application.StartupPath
' fileName = appPath & "\usernames.txt"
' sWriter = New System.IO.StreamWriter(fileName)
' sWriter.Close()
' MessageBox.Show("Writing file to disk")
'Catch ex As Exception
' MessageBox.Show("File Access Error", "Error")
'End Try
'MessageBox.Show("Program terminating")
'Application.Exit()
Hope someone can help! =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要更像这样的东西:
这段代码中值得注意的一些事情:
You want something more like this:
Some things worth noting in this code:
sWriter (streamWriter) 的暗淡声明在哪里?
Where is your dim statement for sWriter (streamWriter)?