如何在 VB.NET 中覆盖文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5030250/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:17:55  来源:igfitidea点击:

How do I overwrite text in VB.NET

vb.nettext-filesoverwrite

提问by Mac

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)?

我曾经教过如何使用以下代码附加文本文件,但是每次按下按钮 1 时如何覆盖该文件(没有人教过我)?

    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

结束班

回答by Shekhar_Pro

Signature of StreamWriter Constructor is this:

StreamWriter 构造函数的签名是这样的:

public StreamWriter(string path,bool append  )

So Change your code :

所以改变你的代码:

System.IO.StreamWriter(ALPHAVAL, True)

to :

到 :

 System.IO.StreamWriter(ALPHAVAL, False)

That tells StreamWriterto Overwrite the file.

这告诉StreamWriter覆盖文件。