简体   繁体   English

如何使用vb.net将文本框值写入.txt文件

[英]How to write textbox values to .txt file with vb.net

I have a simple form with two textboxes, I want Textbox1 to write to a file named C:\\VALUE1.txt and Textbox2 to write its value to a file named C:\\VALUE2.txt 我有一个带有两个文本框的简单表单,我希望Textbox1写入名为C:\\VALUE1.txt的文件,并且Textbox2将其值写入名为C:\\VALUE2.txt的文件

Any text that is already in the text file MUST be over written. 文本文件中已经存在的任何文本都必须被覆盖。

It's worth being familiar with both methods: 值得熟悉这两种方法:

1) In VB.Net you have the quick and easy My.Computer.FileSystem.WriteAllText option: 1)在VB.Net中,您可以轻松快捷地使用My.Computer.FileSystem.WriteAllText选项:

My.Computer.FileSystem.WriteAllText("c:\value1.txt", TextBox1.Text, False)

2) Or else you can go the "long" way round and use the StreamWriter object. 2)否则,您可以走很长一段路,然后使用StreamWriter对象。 Create one as follows - set false in the constructor tells it you don't want to append: 如下创建一个-在构造函数中设置false告诉您您不想追加:

Dim objWriter As New System.IO.StreamWriter("c:\value1.txt", False)

then write text to the file as follows: 然后将文本写入文件,如下所示:

objWriter.WriteLine(Textbox1.Text)
objWriter.Close()
Dim FILE_NAME As String = "C:\VALUE2.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
  Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
  objWriter.Write(TextBox2.Text)
  objWriter.Close()
  MsgBox("Text written to file")
Else
  MsgBox("File Does Not Exist")
End If

看一下System.IOSystem.Text命名空间,尤其是StreamWriter对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM