主题:如何判断文件是否更新?
ictest
[专家分:0] 发布于 2010-06-03 22:00:00
我编写了一个软件,它会每十秒向c:\a.txt写入一个0或者一个1,请注意,是使用了output命令,故a.txt文件的大小是不会改变的。假如我编写的这个软件被关掉或者出错,那么a.txt文件就不会再更新,请问,我如何再编写一个软件实现如下功能:
1,读取a.txt的最后修改时间,赋值给a;
2,隔20秒再读取a.txt的最后修改时间,赋值给b;
3,对比a和b,如不一致,则无提示,如一致,则窗口提示“文件被停止或出错!”
请帮忙写出源码,谢谢!
回复列表 (共3个回复)
沙发
一江秋水 [专家分:9680] 发布于 2010-06-04 18:25:00
你的问题的关键是获取文件修改时间,其它的问题代码都很简单,你自己写吧。
获取文件修改时间必须使用API函数,代码如下:
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Type FILETIME
LowDateTime As Long
HighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 260
cAlternate As String * 14
End Type
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Sub Command1_Click()
Dim ftime As SYSTEMTIME
Dim filedata As WIN32_FIND_DATA
filedata = Findfile("E:\aaa.txt")
FileTimeToSystemTime filedata.ftCreationTime, ftime
Debug.Print "创建时间:" & ftime.wDay & "/" & ftime.wMonth & "/" & ftime.wYear & " " & ftime.wHour & ":" & ftime.wMinute & ":" & ftime.wSecond
FileTimeToSystemTime filedata.ftLastWriteTime, ftime
Debug.Print "最后修改时间:" & ftime.wDay & "/" & ftime.wMonth & "/" & ftime.wYear & " " & ftime.wHour & ":" & ftime.wMinute & ":" & ftime.wSecond
FileTimeToSystemTime filedata.ftLastAccessTime, ftime
Debug.Print "最后访问时间" & ftime.wDay & "/" & ftime.wMonth & "/" & ftime.wYear
End Sub
Private Function Findfile(filename As String) As WIN32_FIND_DATA
Dim Win32Data As WIN32_FIND_DATA
Dim plngFirstFileHwnd As Long
plngFirstFileHwnd = FindFirstFile(filename, Win32Data)
If plngFirstFileHwnd = 0 Then
Findfile.cFileName = "Error"
Else
Findfile = Win32Data
End If
FindClose plngFirstFileHwnd
End Function
板凳
牧狼人小杰 [专家分:30] 发布于 2010-06-15 03:15:00
14óra 7perccel ezeltt Sheng Jiang蒋晟MVP,Moderátor
3 楼
孙瑞 [专家分:590] 发布于 2010-06-24 11:46:00
秋水老师,不知道你的那么多API代码是不是都背下来了?难道我们学习VB变成编程最后要背一大堆API函数,汗!
我来回复