VB.Net - 从文本框写入文本文件

发布于 2024-09-30 19:35:59 字数 895 浏览 1 评论 0原文

嘿伙计们,这里还有一个小问题!尝试为大学作品集编写测验,但在写入 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日暮斜阳 2024-10-07 19:35:59

您想要更像这样的东西:

Dim appPath As String = Application.StartupPath
Dim fileName As String = IO.Path.Combine(appPath, "usernames.txt")

Try
    IO.File.AppendAllText(fileName, TextBox1.Text & Environment.NewLine)
Catch ex As Exception
    MessageBox.Show("File Access Error", "Error")
End Try
MessageBox.Show("Program terminating")

Environment.Exit()

这段代码中值得注意的一些事情:

  • Path.Combine() 作为添加分隔符的正确方法
  • File.AppendAllText() 对于简单的事情来说比弄乱流读取器/写入器要容易得多。将其与另一个方向的 File.ReadAllText() 或 File.ReadAllLines() 配对。
  • Environment.Exit() 与 Application.Exit()

You want something more like this:

Dim appPath As String = Application.StartupPath
Dim fileName As String = IO.Path.Combine(appPath, "usernames.txt")

Try
    IO.File.AppendAllText(fileName, TextBox1.Text & Environment.NewLine)
Catch ex As Exception
    MessageBox.Show("File Access Error", "Error")
End Try
MessageBox.Show("Program terminating")

Environment.Exit()

Some things worth noting in this code:

  • Path.Combine() as the correct way to add the separator character
  • File.AppendAllText() is much easier for simple things than messing with streamreader/writer. Pair it with File.ReadAllText() or File.ReadAllLines() in the other direction.
  • Environment.Exit() vs Application.Exit()
叶落知秋 2024-10-07 19:35:59

sWriter (streamWriter) 的暗淡声明在哪里?

Where is your dim statement for sWriter (streamWriter)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文