用vb如何实现文本框内容按日期和时间备份?

来源:百度知道 编辑:UC知道 时间:2024/05/30 04:35:26
窗体上有一个文本框text1,一个按钮command1。点击command1后,以一个随机生成的txt文件备份text1的内容。注意:每次点击command1后,都生成一个新的以时间(包括年、月、日和时、分、秒)命名的备份文件。

'点击Command1后以时间为文件名保存Text1的内容到程序目录
'如果需要更改存放位置,把 App.Path & "\" 改为相应目录就可以了

Private Sub Command1_Click()
Open App.Path & "\" & Format(Now, "yyyymmddhhmmss") & ".txt" For Append As #1
Print #1, Text1.Text
Close #1
End Sub

我在VB2008下面调试,没有问题。
可以这样做:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Fname As String
Fname = "D:\" & TimetoString() & " .txt" '路径我放在D:\

Dim sw As New IO.StreamWriter(Fname)
sw.WriteLine(Text1.Text)
sw.Close()
End Sub

'用来把当前时间转换为文本的函数timetostring
Private Function TimetoString() As String
Dim nowtime As Date = Now
Dim outstr As String = ""
outstr += nowtime.Year.ToString
outstr += nowtime.Month.ToString
outstr += nowtime.Day.ToString
outstr += nowti