编写VB程序,当程序启动时,生成一条日志记录
答案:2 悬赏:60
解决时间 2021-12-22 18:24
- 提问者网友:逝爱
- 2021-12-22 15:13
内容是启动时间加“启动”两字,如“2008-09-05 23:06:32启动”;当程序退出时,生成一条日志记录,内容是退出时间加“退出”两字,如“2008-09-05 23:06:50退出”。日志文件名为myprog.log,如果文件不存在,则创建文件
最佳答案
- 二级知识专家网友:樣嘚尐年
- 2021-12-22 16:09
'很简单,添加一个过程 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
'在 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
全部回答
- 1楼网友:晚安听书人
- 2021-12-22 17:48
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" For Append As #1
Print #1, Now, "退出"
Print #1, vbNullString'添加一个空行,这句可以不要
Close
End Sub
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯