VB时实写入文件
用VB写日志文件,VB默认是在文件关闭的时候才将文件内容写入磁盘,没关闭的时候写入的内容存在缓冲区里面,请写代码可以将写入的内容时实的写入到磁盘上(内容为文本形式),用A...
用VB写日志文件,VB默认是在文件关闭的时候才将文件内容写入磁盘,没关闭的时候写入的内容存在缓冲区里面,请写代码可以将写入的内容时实的写入到磁盘上(内容为文本形式),用API也可以。
每写入一行就把文件打开并关闭一次的方法不考虑。
写入动作并不连续,基本上每一个函数里面写入一次,但希望每执行一次写入动作就将内容真正写入到磁盘上 展开
每写入一行就把文件打开并关闭一次的方法不考虑。
写入动作并不连续,基本上每一个函数里面写入一次,但希望每执行一次写入动作就将内容真正写入到磁盘上 展开
3个回答
展开全部
Dim FileNum As Long
Private Sub Command1_Click()
Print #FileNum, "按钮单击事件 " & Now
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Print #FileNum, "按钮鼠标移动事件 " & Now
End Sub
Private Sub Form_Load()
FileNum = FreeFile
Open App.Path & "\log.txt" For Append As #FileNum
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Close #FileNum
End Sub
Private Sub Command1_Click()
Print #FileNum, "按钮单击事件 " & Now
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Print #FileNum, "按钮鼠标移动事件 " & Now
End Sub
Private Sub Form_Load()
FileNum = FreeFile
Open App.Path & "\log.txt" For Append As #FileNum
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Close #FileNum
End Sub
追问
这个程序没退出前,文件里面都是空的,并没有真正写在磁盘上
追答
你好好测试下我的代码在说 OK?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以考虑用ini文件,这个可以实时写入,而且不用打开关闭文件,代码如下:
'--------cIniFile.cls 代码----------------
'这里定义了一个cIniFile类
Option Explicit
'// Private member that holds a reference to
'// the path of our ini file
Private strInI As String
'// Win API Declares
Private Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString _
Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long
Private Function MakePath(ByVal strDrv As String, _
ByVal strDir As String) As String
'// Makes an INI file: Guarantees a sub dir
Do While Right$(strDrv, 1) = "\"
strDrv = Left$(strDrv, Len(strDrv) - 1)
Loop
Do While Left$(strDir, 1) = "\"
strDir = Mid$(strDir, 2)
Loop
'// Return the path
MakePath = strDrv & "\" & strDir
End Function
Public Sub CreateIni(strDrv As String, strDir As String)
'// Make a new ini file
strInI = MakePath(strDrv, strDir)
End Sub
Public Sub WriteFile(strSection As String, _
strKey As String, _
strValue As String)
'// Write to strINI
WritePrivateProfileString strSection, _
strKey, strValue, strInI
End Sub
Public Function GetFile(strSection As String, _
strKey As String) As String
Dim strTmp As String
Dim lngRet As String
strTmp = String$(100, Chr(32))
lngRet = GetPrivateProfileString(strSection, _
strKey, "", strTmp, _
Len(strTmp), strInI)
'strUser = Trim(myIniFile.GetFile("setting", "username"))
'strUser = Left(strUser, Len(strUser) - 1)
strTmp = Trim(strTmp)
strTmp = Left(strTmp, Len(strTmp) - 1)
GetFile = strTmp
End Function
Public Property Let INIFile(ByVal New_IniPath As String)
'// Sets the new ini path
strInI = New_IniPath
End Property
Public Property Get INIFile() As String
'// Returns the current ini path
INIFile = strInI
End Property
'--------cIniFile.cls 使用举例----------------
' Dim myIniFile As New cIniFile
'---指定访问的ini文件
' If Len(App.Path) > 3 Then
'under disk root dir , eg: "C:\"
' myIniFile.INIFile = App.Path & "\setting.ini"
' Else
' myIniFile.INIFile = App.Path & "setting.ini"
' End If
'---写入ini文件
' myIniFile.WriteFile "setting", "username", strUser
'---读出ini文件的数据
' 注意,如果是字符串,则去掉末尾一个字符
' ----flybird@chinaasp.com
' strUser = Trim(myIniFile.GetFile("setting", "username"))
' strUser = Left(strUser, Len(strUser) - 1)
'--------cIniFile.cls 代码----------------
'这里定义了一个cIniFile类
Option Explicit
'// Private member that holds a reference to
'// the path of our ini file
Private strInI As String
'// Win API Declares
Private Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString _
Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long
Private Function MakePath(ByVal strDrv As String, _
ByVal strDir As String) As String
'// Makes an INI file: Guarantees a sub dir
Do While Right$(strDrv, 1) = "\"
strDrv = Left$(strDrv, Len(strDrv) - 1)
Loop
Do While Left$(strDir, 1) = "\"
strDir = Mid$(strDir, 2)
Loop
'// Return the path
MakePath = strDrv & "\" & strDir
End Function
Public Sub CreateIni(strDrv As String, strDir As String)
'// Make a new ini file
strInI = MakePath(strDrv, strDir)
End Sub
Public Sub WriteFile(strSection As String, _
strKey As String, _
strValue As String)
'// Write to strINI
WritePrivateProfileString strSection, _
strKey, strValue, strInI
End Sub
Public Function GetFile(strSection As String, _
strKey As String) As String
Dim strTmp As String
Dim lngRet As String
strTmp = String$(100, Chr(32))
lngRet = GetPrivateProfileString(strSection, _
strKey, "", strTmp, _
Len(strTmp), strInI)
'strUser = Trim(myIniFile.GetFile("setting", "username"))
'strUser = Left(strUser, Len(strUser) - 1)
strTmp = Trim(strTmp)
strTmp = Left(strTmp, Len(strTmp) - 1)
GetFile = strTmp
End Function
Public Property Let INIFile(ByVal New_IniPath As String)
'// Sets the new ini path
strInI = New_IniPath
End Property
Public Property Get INIFile() As String
'// Returns the current ini path
INIFile = strInI
End Property
'--------cIniFile.cls 使用举例----------------
' Dim myIniFile As New cIniFile
'---指定访问的ini文件
' If Len(App.Path) > 3 Then
'under disk root dir , eg: "C:\"
' myIniFile.INIFile = App.Path & "\setting.ini"
' Else
' myIniFile.INIFile = App.Path & "setting.ini"
' End If
'---写入ini文件
' myIniFile.WriteFile "setting", "username", strUser
'---读出ini文件的数据
' 注意,如果是字符串,则去掉末尾一个字符
' ----flybird@chinaasp.com
' strUser = Trim(myIniFile.GetFile("setting", "username"))
' strUser = Left(strUser, Len(strUser) - 1)
追问
我的日志文件不想写成 n=xxxxxxxx 这种形式的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个你的告诉我你是用哪个接口写文件的。
如果是windows api,你可以考虑这个
FlushFileBuffers
如果是windows api,你可以考虑这个
FlushFileBuffers
追问
我用的是VB的OPEN 打开文件的,不行,FlushFileBuffers 这个API好像能行,但请给出完整的代码
追答
FlushFileBuffers 就一个参数,就是你的文件句柄。
BOOL FlushFileBuffers( HANDLE hFile);
没有flush以前在内存,flush就是把内存的东西放在磁盘上。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询