vb.net 里怎样才能只写入一行到指定文件里
我想把Txt1中的字符串写入路径为E:\Study\test文件名为"梦语.txt"的文件中注:"梦语.txt"的文件中已有一些内容,只想在最下面的一行加入Txt1的字符...
我想把Txt1中的字符串写入路径为E:\Study\test文件名为"梦语.txt"的文件中
注:"梦语.txt"的文件中已有一些内容,只想在最下面的一行加入Txt1的字符串,有办法吗?请各位大侠们赐教啊!谢啦,分不能问题啊! 展开
注:"梦语.txt"的文件中已有一些内容,只想在最下面的一行加入Txt1的字符串,有办法吗?请各位大侠们赐教啊!谢啦,分不能问题啊! 展开
2014-03-08
展开全部
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bool As Boolean = False
bool = wirte("梦语和启.txt", "新内容哪明!!!")
If bool Then
MessageBox.Show("数据已写入")
Else
MessageBox.Show("写入数据时李棚告出错")
End If
End Sub
'FilePath 为文件路径
'Str要写入的内容
Public Function wirte(ByVal FilePath As String, ByVal Str As String) As Boolean
Dim sr As StreamReader
Dim sw As StreamWriter
Try
sr = New StreamReader(FilePath)
Dim oldStr As String = sr.ReadToEnd()
sr.Close()
sw = New StreamWriter(FilePath)
'判断以前的文本是否有记录
If oldStr.Length > 0 Then
'写入以前的数据
sw.WriteLine(oldStr)
End If
'在文件尾部添加新的内容
sw.Write(Str)
sw.Close()
Return True
Catch ex As Exception
Return False
Finally
If sr IsNot Nothing Then
sr.Close()
End If
If sw IsNot Nothing Then
sw.Close()
End If
End Try
End Function
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bool As Boolean = False
bool = wirte("梦语和启.txt", "新内容哪明!!!")
If bool Then
MessageBox.Show("数据已写入")
Else
MessageBox.Show("写入数据时李棚告出错")
End If
End Sub
'FilePath 为文件路径
'Str要写入的内容
Public Function wirte(ByVal FilePath As String, ByVal Str As String) As Boolean
Dim sr As StreamReader
Dim sw As StreamWriter
Try
sr = New StreamReader(FilePath)
Dim oldStr As String = sr.ReadToEnd()
sr.Close()
sw = New StreamWriter(FilePath)
'判断以前的文本是否有记录
If oldStr.Length > 0 Then
'写入以前的数据
sw.WriteLine(oldStr)
End If
'在文件尾部添加新的内容
sw.Write(Str)
sw.Close()
Return True
Catch ex As Exception
Return False
Finally
If sr IsNot Nothing Then
sr.Close()
End If
If sw IsNot Nothing Then
sw.Close()
End If
End Try
End Function
End Class
2014-03-08
展开全部
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fs As New StreamWriter("E:\Study\test\梦族轿语缺穗纳.txt", True) '伏没以在最后追加方式打开文件
fs.WriteLine(TextBox1.Text) '写入textbox1的内容
fs.Close() '关闭文件
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fs As New StreamWriter("E:\Study\test\梦族轿语缺穗纳.txt", True) '伏没以在最后追加方式打开文件
fs.WriteLine(TextBox1.Text) '写入textbox1的内容
fs.Close() '关闭文件
End Sub
End Class
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-08
展开全部
Button1_Click为册梁按纽事件
wirte(ByVal FilePath As String, ByVal Str As String) As Boolean为写穗散文件的方猜姿氏法
wirte(ByVal FilePath As String, ByVal Str As String) As Boolean为写穗散文件的方猜姿氏法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询