判斷一個檔案是否在IE的快取中 (轉)

worldblog發表於2007-12-25
判斷一個檔案是否在IE的快取中 (轉)[@more@]  當你建立一個聯到網上的快捷方式時,你可能需要知道它是否已經被訪問過,於是你就可以適當地改變連結的顏色等等。這則小技巧
就是告訴你如何判斷一個檔案是否在Inte 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
  lpszUrlName 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

'用來報告產生的錯誤:
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 dguageId 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地址(比如,)當你按下Command按鈕時,就會得到一則訊息報告URL在不在快取中。如果給出了URL沒找到的原因,它將被顯示在隨後的的方括號內。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-995722/,如需轉載,請註明出處,否則將追究法律責任。

相關文章