判断一个文件是否在IE的缓存中 (转)

判断一个文件是否在IE的缓存中 (转)[@more@]  当你建立一个联到网上 文件的快捷方式时,你可能需要知道它是否已经被访问过,于是你就可以适当地改变链接的颜色等等。这则小技巧
就是告诉你如何判断一个文件是否在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
  lpsz sourceUrlName 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 d WlanguageId 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没找到的原因,它将被显示在随后的的方括号内。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10790690/viewspace-951765/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10790690/viewspace-951765/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值