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

发表于:2007-07-14来源:作者:点击数: 标签:
当你建立一个联到网上文件的快捷方式时,你可能需要知道它是否已经被访问过,于是你就可以适当地改变链接的颜色等等。这则小技巧 就是告诉你如何判断一个文件是否在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没找到的原因,它将被显示在随后的的方括号内。

原文转自:http://www.ltesting.net