如何在 VB.NET 中覆盖文本
我曾经被教导如何使用以下代码附加文本文件,但是每次按下按钮一时如何覆盖该文件(没有人教我)?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ALPHAVAL As String = "C:\ALPHAVAL.txt"
If System.IO.File.Exists(ALPHAVAL) = True Then
Dim objWriter As New System.IO.StreamWriter(ALPHAVAL, True)
objWriter.WriteLine(TextBox1.Text)
objWriter.Close()
End If
下课
I once got taught how to append a text file ussing the following code, but how do I overwrite the file every time I press button one (nobody taught me that)?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ALPHAVAL As String = "C:\ALPHAVAL.txt"
If System.IO.File.Exists(ALPHAVAL) = True Then
Dim objWriter As New System.IO.StreamWriter(ALPHAVAL, True)
objWriter.WriteLine(TextBox1.Text)
objWriter.Close()
End If
End class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
StreamWriter 构造函数的签名是这样的:
因此,将代码 : 更改
为 : ,
这告诉 StreamWriter 覆盖文件。
Signature of StreamWriter Constructor is this:
So Change your code :
to :
That tells StreamWriter to Overwrite the file.