编写VB程序,当程序启动时,生成一条日志记录

来源:百度知道 编辑:UC知道 时间:2024/06/03 01:39:02
内容是启动时间加“启动”两字,如“2008-09-05 23:06:32启动”;当程序退出时,生成一条日志记录,内容是退出时间加“退出”两字,如“2008-09-05 23:06:50退出”。日志文件名为myprog.log,如果文件不存在,则创建文件

'很简单,添加一个过程 MyLog
'在 Form_Load 中加入 Call MyLog("启动")
'在 Form_Unload 中加入 Call MyLog("退出")

Private Sub Form_Load()
Call MyLog("启动")
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call MyLog("退出")
End Sub

Private Sub MyLog(nStr As String)
Dim F As String, H As Long

nStr = Format(Now, "yyyy-mm-dd hh:mm:ss") & nStr
F = "C:\myprog.log"
'如果要将文件保存到与你的 exe 相同的文件夹,将上句改为 F = App.Path & "\myprog.log"

H = FreeFile
Open F For Append As #H
Print #H, nStr
Close #H
End Sub

Private Sub Form_Load()
Open "d:\myprog.log" For Append As #1
Print #1, Now, "启动" '可以把now后面的逗号改成分号,紧凑输出
Close
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Open "d:\myprog.log&