资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vb.net写文件,net vb

请教,vb.net写入文件问题,为什么我用两种办法写文件只能写入最后一行

写入模式为覆盖会出现这个现象

成都创新互联专业为企业提供浚县网站建设、浚县做网站、浚县网站设计、浚县网站制作等企业网站建设、网页设计与制作、浚县企业网站模板建站服务,十载浚县做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

要使用添加模式

就会累加了

请教在VB.net中如何将数据写入txt文件、再从txt文件读出?

软糖来告诉你吧。

VB.net中读写文件主要使用System.IO命名空间。

① 使用 File.ReadAllText 读取

Dim s As String = System.IO.File.ReadAllText("C:\a.txt")

② 使用 StreamReader 读取,注意编码格式和写入的编码保持一致。

Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)

Dim s As String = sr.ReadToEnd()

sr.Close()

③ 使用 File.WriteAllText 写入,会覆盖同名的文件。

Dim 要写的内容 As String = ""

File.WriteAllText(文件路径, 要写的内容, System.Text.Encoding.UTF8)

④ 使用 StreamWriter 写入。

Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("C:\a.txt", False, System.Text.Encoding.UTF8)

sw.WriteLine(TextTB.Text)

sw.Close()

⑤ 使用 StreamWriter 追加写入。

将上面代码的第二个参数False改为True。

◆ 满意请采纳,谢谢 ◆

vb.net中,读取和写入文件

写入:Dim sr As New IO.StreamWriter(Application.StartupPath "/写入的文本.txt")

sr.WriteLine("写入的内容") sr.Close()读取:If (File.Exists(Application.StartupPath "/msg.txt")) Then

Dim fm As New IO.FileStream(Application.StartupPath "/读取的文本.txt", FileMode.Open)

Dim sr As IO.StreamReader = New IO.StreamReader(fm)

Do While sr.Peek() = 0

TextBox1.Text = sr.ReadLine() (读取文本到文本框)

Loop end if

vb.net怎么往已经建立好的dat文件里写东西,还不覆盖原本的数据?

如果想继续编辑之前的文档,在TXT文件尾部继续添加文本,那么还需要在函数后边加个参数。

VB 代码

方法1:

  Dim sw As StreamWriter = New StreamWriter("C:\temp\test.txt")

  sw.Write("abc"  vbCrLf)

sw.Close()

Dim sw2 As StreamWriter = New StreamWriter("C:\temp\test.txt", True)

sw2.Write("456"  vbCrLf)

sw2.Close()

方法2:

  My.Computer.FileSystem.WriteAllText("test.txt", "This is test Text", True)

方法3:

  System.IO.File.AppendAllText("c:\temp\test.txt", "this is extra test file")

详见:“网页链接” 

关于VB.NET的写入文件

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

Dim aa As System.IO.StreamWriter = New System.IO.StreamWriter("C:\a.txt", False, System.Text.Encoding.UTF8)

Dim i As Integer = 0

For i = 0 To Val(TextBox4.Text)

aa.WriteLine("[" i "]")

aa.WriteLine("[" i "]")

Next

aa.Close()

aa.Dispose()

End Sub


分享名称:vb.net写文件,net vb
链接URL:http://cdkjz.cn/article/dsidhpe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220