欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现清除IE浏览器缓存的方法

程序员文章站 2023-11-16 23:21:10
本文实例讲述了c#实现清除ie浏览器缓存的方法。分享给大家供大家参考。具体如下: 项目中碰到wpf webbrowser的几个问题,在此记录一下 1.webbrowse...

本文实例讲述了c#实现清除ie浏览器缓存的方法。分享给大家供大家参考。具体如下:

项目中碰到wpf webbrowser的几个问题,在此记录一下

1.webbrowser中对于jquery的bind事件的处理.

在普通的浏览器下一下这种写法没有任何问题

  var content = $("<div><h4><span>" + category_name + "</span>(<a id='href_" + guid + "' href='addoreditshowinfo.aspx?category=" + guid + "'>添加展示</a>)" +
  "<span id='edit_" + guid + "' style='font-size:12px;cursor:pointer;' onclick='showcategory(this, \""+guid+"\")'>修改分类</span>  " +
  "<span id='del_" + guid + "' style='font-size:12px;cursor:pointer;' onclick=delcategory(this, \""+guid+"\")'>删除分类</span></h4>" +
  "<table class='gridview' cellspacing='0' rules='all' border='1' id='gvdata' width='100%'>" +
  "<thead><tr><th>缩略图</th><th>展示名称</th><th>简介</th><th>详细描述</th><th>操作</th></tr></thead>" +
  "<tbody id='t_" + guid + "' class='css_tbody'></tbody></table></div>"
);
$("#vtab").append(content);

但是在webbrowser中事件就会不响应,把content中的onclick去掉,在下面这样绑定:

$("#edit_" + guid).unbind("click").bind("click", function () { showcategory(this, guid) });
$("#del_" + guid).unbind("click").bind("click", function () { delcategory(this,guid)});

2.在webbrowser中使用jquery uploadify上传组件的问题

使用该组件的时候 ,发现上传图片的时候 ,第一次上传的时候没有任何问题,第二次上传的时候会出现无法上传的情况,没有任何反应,没有任何错误,上传进度不动,上传的后台代码也不能触发.

解决方案是:清空浏览器缓存就ok 了.下面就介绍代码清空缓存的方法

3. 清理ie缓存的方法

很明显 ie的缓存在其目录中显示的并不是真正的文件所处的位置,文件的位置是在隐藏的文件夹中,而且这个隐藏的文件夹我们一般找不到.在网上几种清空缓存的方法,在此我一一把代码和处理的效果显示出来.供大家参考.

①.使用ie缓存路径来删除缓存的

string cachepath = environment.getfolderpath(environment.specialfolder.internetcache);
//获取缓存路径
directoryinfo di = new directoryinfo(cachepath);
foreach (fileinfo fi in di.getfiles("*.*", searchoption.alldirectories))//遍历所有的文件夹 删除里面的文件
{
  try
  {
   fi.delete();
  }
  catch { }
}

效果:并没有真正的删除缓存文件.而且会出现很多异常,比如enguser.dat,index.dat,,,这些文件删除的时候会出现另一个程序还在使用的错误

②.调用winnet.dll 清理缓存 上代码

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.rntime.interopservices;
using system.io;
namespace wpfclient.appcode
{
 public class clearcache
 {
  [structlayout(layoutkind.explicit, size = 80,charset=charset.auto)]
  protected struct internet_cache_entry_infoa
  {
   [fieldoffset(0)]
   public uint dwstructsize;
   [fieldoffset(4)]
   public intptr lpszsourceurlname;
   [fieldoffset(8)]
   public intptr lpszlocalfilename;
   [fieldoffset(12)]
   public uint cacheentrytype;
   [fieldoffset(16)]
   public uint dwusecount;
   [fieldoffset(20)]
   public uint dwhitrate;
   [fieldoffset(24)]
   public uint dwsizelow;
   [fieldoffset(28)]
   public uint dwsizehigh;
   [fieldoffset(32)]
   public filetime lastmodifiedtime;
   [fieldoffset(40)]
   public filetime expiretime;
   [fieldoffset(48)]
   public filetime lastaccesstime;
   [fieldoffset(56)]
   public filetime lastsynctime;
   [fieldoffset(64)]
   public intptr lpheaderinfo;
   [fieldoffset(68)]
   public uint dwheaderinfosize;
   [fieldoffset(72)]
   public intptr lpszfileextension;
   [fieldoffset(76)]
   public uint dwreserved;
   [fieldoffset(76)]
   public uint dwexemptdelta;
  }
  // for pinvoke: initiates the enumeration of the cache groups in the internet cache
  [dllimport(@"wininet",
   setlasterror = true,
   charset = charset.auto,
   entrypoint = "findfirsturlcachegroup",
   callingconvention = callingconvention.stdcall)]
  protected static extern intptr findfirsturlcachegroup(
   int dwflags,
   int dwfilter,
   intptr lpsearchcondition,
   int dwsearchcondition,
   ref long lpgroupid,
   intptr lpreserved);
  // for pinvoke: retrieves the next cache group in a cache group enumeration
  [dllimport(@"wininet",
   setlasterror = true,
   charset = charset.auto,
   entrypoint = "findnexturlcachegroup",
   callingconvention = callingconvention.stdcall)]
  protected static extern bool findnexturlcachegroup(
   intptr hfind,
   ref long lpgroupid,
   intptr lpreserved);
  // for pinvoke: releases the specified groupid and any associated state in the cache index file
  [dllimport(@"wininet",
   setlasterror = true,
   charset = charset.auto,
   entrypoint = "deleteurlcachegroup",
   callingconvention = callingconvention.stdcall)]
  protected static extern bool deleteurlcachegroup(
   long groupid,
   int dwflags,
   intptr lpreserved);
  // for pinvoke: begins the enumeration of the internet cache
  [dllimport(@"wininet",
   setlasterror = true,
   charset = charset.auto,
   entrypoint = "findfirsturlcacheentrya",
   callingconvention = callingconvention.stdcall)]
  protected static extern intptr findfirsturlcacheentry(
   [marshalas(unmanagedtype.lptstr)] string lpszurlsearchpattern,
   intptr lpfirstcacheentryinfo,
   ref int lpdwfirstcacheentryinfobuffersize);
  // for pinvoke: retrieves the next entry in the internet cache
  [dllimport(@"wininet",
   setlasterror = true,
   charset = charset.auto,
   entrypoint = "findnexturlcacheentrya",
   callingconvention = callingconvention.stdcall)]
  protected static extern bool findnexturlcacheentry(
   intptr hfind,
   intptr lpnextcacheentryinfo,
   ref int lpdwnextcacheentryinfobuffersize);
  // for pinvoke: removes the file that is associated with the source name from the cache, if the file exists
  [dllimport(@"wininet",
   setlasterror = true,
   charset = charset.auto,
   entrypoint = "deleteurlcacheentrya",
   callingconvention = callingconvention.stdcall)]
  protected static extern bool deleteurlcacheentry(
   intptr lpszurlname)
  public static void delcache(){
   // indicates that all of the cache groups in the user's system should be enumerated
   const int cachegroup_search_all = 0x0;
   // indicates that all the cache entries that are associated with the cache group
   // should be deleted, unless the entry belongs to another cache group.
   const int cachegroup_flag_flushurl_ondelete = 0x2;
   // file not found.
   const int error_file_not_found = 0x2;
   // no more items have been found.
   const int error_no_more_items = 259;
   // pointer to a groupid variable
   long groupid = 0;
   // local variables
   int cacheentryinfobuffersizeinitial = 0;
   int cacheentryinfobuffersize = 0;
   intptr cacheentryinfobuffer = intptr.zero;
   internet_cache_entry_infoa internetcacheentry;
   intptr enumhandle = intptr.zero;
   bool returnvalue = false
   // delete the groups first.
   // groups may not always exist on the system.
   // for more information, visit the following microsoft web site:
   // http://msdn.microsoft.com/library/?url=/workshop/networking/wininet/overview/cache.asp  
   // by default, a url does not belong to any group. therefore, that cache may become
   // empty even when the cachegroup apis are not used because the existing url does not belong to any group.   
   enumhandle = findfirsturlcachegroup(0, cachegroup_search_all, intptr.zero, 0, ref groupid, intptr.zero);
   // if there are no items in the cache, you are finished.
   if (enumhandle != intptr.zero && error_no_more_items == marshal.getlastwin32error())
    return;
   // loop through cache group, and then delete entries.
   while(true)
   {
    // delete a particular cache group.
    returnvalue = deleteurlcachegroup(groupid, cachegroup_flag_flushurl_ondelete, intptr.zero);
    if (!returnvalue && error_file_not_found == marshal.getlastwin32error())
    {
     returnvalue = findnexturlcachegroup(enumhandle, ref groupid, intptr.zero);
    }
    if (!returnvalue && (error_no_more_items == marshal.getlastwin32error() || error_file_not_found == marshal.getlastwin32error()))
     break;
   }
   // start to delete urls that do not belong to any group.
   enumhandle = findfirsturlcacheentry(null, intptr.zero, ref cacheentryinfobuffersizeinitial);
   if (enumhandle == intptr.zero && error_no_more_items == marshal.getlastwin32error())
    return;
   cacheentryinfobuffersize = cacheentryinfobuffersizeinitial;
   cacheentryinfobuffer = marshal.allochglobal(cacheentryinfobuffersize);
   enumhandle = findfirsturlcacheentry(null, cacheentryinfobuffer, ref cacheentryinfobuffersizeinitial);
   while(true)
   {
    internetcacheentry = (internet_cache_entry_infoa)marshal.ptrtostructure(cacheentryinfobuffer, typeof(internet_cache_entry_infoa));  
    cacheentryinfobuffersizeinitial = cacheentryinfobuffersize;
    returnvalue = deleteurlcacheentry(internetcacheentry.lpszsourceurlname);
    string s = marshal.ptrtostringansi(internetcacheentry.lpszlocalfilename);
    if (!returnvalue)
    {
     returnvalue = findnexturlcacheentry(enumhandle, cacheentryinfobuffer, ref cacheentryinfobuffersizeinitial);
    }
    if (!returnvalue && error_no_more_items == marshal.getlastwin32error())
    {
     break;
    }
    if (!returnvalue && cacheentryinfobuffersizeinitial > cacheentryinfobuffersize)
    {
     cacheentryinfobuffersize = cacheentryinfobuffersizeinitial;
     cacheentryinfobuffer = marshal.reallochglobal(cacheentryinfobuffer, (intptr)cacheentryinfobuffersize);
     returnvalue = findnexturlcacheentry(enumhandle, cacheentryinfobuffer, ref cacheentryinfobuffersizeinitial);
    }
   }
   marshal.freehglobal(cacheentryinfobuffer);  
  }
 }
}

效果:总体来说还是有点效果的,但是效率极低,会出现长时间的等待情况,程序假死. 最重要的是不知道什么时候结束.

③.调用rundll32.exe

runcmd("rundll32.exe inetcpl.cpl,clearmytracksbyprocess 8");
void runcmd(string cmd)
{
 system.diagnostics.process p = new system.diagnostics.process();
 p.startinfo.filename = "cmd.exe";
 // 关闭shell的使用
 p.startinfo.useshellexecute = false;
 // 重定向标准输入
 p.startinfo.redirectstandardinput = true;
 // 重定向标准输出
 p.startinfo.redirectstandardoutput = true;
 //重定向错误输出
 p.startinfo.redirectstandarderror = true;
 p.startinfo.createnowindow = true;
 p.start();
 p.standardinput.writeline(cmd);
 p.standardinput.writeline("exit");
}

效果: 这个方法解决的我的问题,缓存被清空.

以下是其他一些参数的说明:

//temporary internet files (internet临时文件)
//rundll32.exe inetcpl.cpl,clearmytracksbyprocess 8
//cookies
//rundll32.exe inetcpl.cpl,clearmytracksbyprocess 2
//history (历史记录)
//rundll32.exe inetcpl.cpl,clearmytracksbyprocess 1
//form data (表单数据)
//rundll32.exe inetcpl.cpl,clearmytracksbyprocess 16
//passwords (密码)
//rundll32.exe inetcpl.cpl,clearmytracksbyprocess 32
//delete all (全部删除)
//rundll32.exe inetcpl.cpl,clearmytracksbyprocess 255
//delete all - "also delete files and settings stored by add-ons"
//rundll32.exe inetcpl.cpl,clearmytracksbyprocess 4351

希望本文所述对大家的c#程序设计有所帮助。