飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 3474|回复: 2

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

[复制链接]
  • TA的每日心情
    开心
    2019-9-19 16:05
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    发表于 2007-2-12 17:59:35 | 显示全部楼层 |阅读模式
    当你建立一个联到网上文件的快捷方式时,你可能需要知道它是否已经被访问过,于是你就可以适当地改变链接的颜色等等。这则小技巧

    就是告诉你如何判断一个文件是否在Internet 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没找到的原因,它将被显示在随后的的方括号内。
    -----------------------------------------------------
    PYG19周年生日快乐!

    该用户从未签到

    发表于 2007-2-12 18:04:30 | 显示全部楼层
    又改VB了,全面啊。。。。。。。。。。。:victory:
    PYG19周年生日快乐!
  • TA的每日心情
    开心
    2019-9-19 16:05
  • 签到天数: 4 天

    [LV.2]偶尔看看I

     楼主| 发表于 2007-2-12 18:07:46 | 显示全部楼层
    没有今天忘了怎么返回系统错误描述了,找了找发现了2个VB的例子,放到这里查找。
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表