当你建立一个联到网上文件的快捷方式时,你可能需要知道它是否已经被访问过,于是你就可以适当地改变链接的颜色等等。这则小技巧 就是告诉你如何判断一个文件是否在Inte .net Explorer的缓存中,以满足你的须要。 新建一个项目、添加一个模块。将以下代码写
当你建立一个联到网上文件的快捷方式时,你可能需要知道它是否已经被访问过,于是你就可以适当地改变链接的颜色等等。这则小技巧
就是告诉你如何判断一个文件是否在Inte
.net Explorer的缓存中,以满足你的须要。
新建一个项目、添加一个模块。将以下代码写到模块里:
Private Const ERROR_INSUFFICIENT_BUFFER = 122
Private Const eeErrorBase = 26720
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type INTERNET_CACHE_ENTRY_INFO
dwStructSize As Long
lpszSourceUrlName As String
lpszLocalFileName As String
CacheEntryType As String
dwUseCount As Long
dwHitRate As Long
dwSizeLow As Long
dwSizeHigh As Long
LastModifiedTime As FILETIME
ExpireTIme As FILETIME
LastAccessTime As FILETIME
LastSyncTime As FILETIME
lpHeaderInfo As Long
dwHeaderInfoSize As Long
lpszFileExtension As String
dwReserved As Long
End Type
Private Declare Function GetUrlCacheEntryInfo Lib "wininet.dll" _
Alias "GetUrlCacheEntryInfoA" (ByVal sUrlName As String, _
lpCacheEntryInfo As Any, lpdwCacheEntryInfoBufferSize _
As Long) As Long
'用来报告API产生的错误:
Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Private Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
Private Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Private Const FORMAT_MESSAGE_FROM_STRING = &H400
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Private Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF
Private Declare Function FormatMessage Lib "kernel32" Alias _
"FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, _
ByVal dwMessageId As Long, ByVal dwLanguageId As Long, _
ByVal lpBuffer As String, ByVal nSize As Long, Arguments _
As Long) As Long
Public Function WinAPIError(ByVal lLastDLLError As Long) As String
Dim sBuff As String
Dim lCount As Long
'返回与LastDLLError相关的出错信息:
sBuff = String$(256, 0)
lCount = FormatMessage( _
FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, _
0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0)
If lCount Then
WinAPIError = Left$(sBuff, lCount)
End If
End Function
Public Function GetCacheEntryInfo(ByVal hWnd As Long, _
ByVal lpszUrl As String) As Boolean
Dim dwEntrySize As Long
Dim lpCacheEntry As INTERNET_CACHE_ENTRY_INFO
Dim dwTemp As Long
Dim lErr As Long
If (GetUrlCacheEntryInfo(lpszUrl, ByVal 0&, dwEntrySize)) = 0 Then
lErr = Err.LastDllError
If (lErr <> ERROR_INSUFFICIENT_BUFFER) Then
'预料外的错误。须要显示出错原因:
Err.Raise eeErrorBase + 1, _
App.EXEName & ".mCacheEntry", WinAPIError(lErr)
GetCacheEntryInfo = False
Exit Function
Else
'这是预料中的错误
GetCacheEntryInfo = True
End If
End If
End Function
在窗体上添加一个Command和一个Text。然后加入这些代码:
Option Explicit
Private Sub Command1_Click()
On Error Goto ErrorHandler
'检查Text中的URL地址是否出现在缓存中:
If (GetCacheEntryInfo(Me.hWnd, Text1.Text)) Then
MsgBox "URL In Cache.", vbInformation
Else
MsgBox "URL Not In Cache.", vbInformation
End If
Exit Sub
ErrorHandler:
MsgBox "URL Not in Cache [" & Err.Description & "]",vbInformation
End Sub
运行,在Text中键入一个URL地址(比如,http://www.vbaccelerator.com/index.html)当你按下Command按钮时,就会得到一则消息报告URL在不在缓存中。如果
Windows给出了URL没找到的原因,它将被显示在随后的的方括号内。