C#实现清除IE浏览器缓存的方法
本文实例讲述了C#实现清除IE浏览器缓存的方法。分享给大家供大家参考。具体如下:
项目中碰到wpfwebbrowser的几个问题,在此记录一下
1.webbrowser中对于jquery的bind事件的处理.
在普通的浏览器下一下这种写法没有任何问题
varcontent=$("<div><h4><span>"+category_name+"</span>(<aid='href_"+guid+"'href='AddOrEditShowInfo.aspx?Category="+guid+"'>添加展示</a>)"+
"<spanid='edit_"+guid+"'style='font-size:12px;cursor:pointer;'onclick='showCategory(this,\""+guid+"\")'>修改分类</span> "+
"<spanid='del_"+guid+"'style='font-size:12px;cursor:pointer;'onclick=delCategory(this,\""+guid+"\")'>删除分类</span></h4>"+
"<tableclass='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>"+
"<tbodyid='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中使用jqueryuploadify上传组件的问题
使用该组件的时候,发现上传图片的时候,第一次上传的时候没有任何问题,第二次上传的时候会出现无法上传的情况,没有任何反应,没有任何错误,上传进度不动,上传的后台代码也不能触发.
解决方案是:清空浏览器缓存就Ok了.下面就介绍代码清空缓存的方法
3.清理IE缓存的方法
很明显IE的缓存在其目录中显示的并不是真正的文件所处的位置,文件的位置是在隐藏的文件夹中,而且这个隐藏的文件夹我们一般找不到.在网上几种清空缓存的方法,在此我一一把代码和处理的效果显示出来.供大家参考.
①.使用ie缓存路径来删除缓存的
stringcachePath=Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
//获取缓存路径
DirectoryInfodi=newDirectoryInfo(cachePath);
foreach(FileInfofiindi.GetFiles("*.*",SearchOption.AllDirectories))//遍历所有的文件夹删除里面的文件
{
try
{
fi.Delete();
}
catch{}
}
效果:并没有真正的删除缓存文件.而且会出现很多异常,比如enguser.dat,index.dat,,,这些文件删除的时候会出现另一个程序还在使用的错误
②.调用winnet.dll清理缓存上代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Rntime.InteropServices;
usingSystem.IO;
namespaceWpfClient.AppCode
{
publicclassClearCache
{
[StructLayout(LayoutKind.Explicit,Size=80,CharSet=CharSet.Auto)]
protectedstructINTERNET_CACHE_ENTRY_INFOA
{
[FieldOffset(0)]
publicuintdwStructSize;
[FieldOffset(4)]
publicIntPtrlpszSourceUrlName;
[FieldOffset(8)]
publicIntPtrlpszLocalFileName;
[FieldOffset(12)]
publicuintCacheEntryType;
[FieldOffset(16)]
publicuintdwUseCount;
[FieldOffset(20)]
publicuintdwHitRate;
[FieldOffset(24)]
publicuintdwSizeLow;
[FieldOffset(28)]
publicuintdwSizeHigh;
[FieldOffset(32)]
publicFILETIMELastModifiedTime;
[FieldOffset(40)]
publicFILETIMEExpireTime;
[FieldOffset(48)]
publicFILETIMELastAccessTime;
[FieldOffset(56)]
publicFILETIMELastSyncTime;
[FieldOffset(64)]
publicIntPtrlpHeaderInfo;
[FieldOffset(68)]
publicuintdwHeaderInfoSize;
[FieldOffset(72)]
publicIntPtrlpszFileExtension;
[FieldOffset(76)]
publicuintdwReserved;
[FieldOffset(76)]
publicuintdwExemptDelta;
}
//ForPInvoke:InitiatestheenumerationofthecachegroupsintheInternetcache
[DllImport(@"wininet",
SetLastError=true,
CharSet=CharSet.Auto,
EntryPoint="FindFirstUrlCacheGroup",
CallingConvention=CallingConvention.StdCall)]
protectedstaticexternIntPtrFindFirstUrlCacheGroup(
intdwFlags,
intdwFilter,
IntPtrlpSearchCondition,
intdwSearchCondition,
reflonglpGroupId,
IntPtrlpReserved);
//ForPInvoke:Retrievesthenextcachegroupinacachegroupenumeration
[DllImport(@"wininet",
SetLastError=true,
CharSet=CharSet.Auto,
EntryPoint="FindNextUrlCacheGroup",
CallingConvention=CallingConvention.StdCall)]
protectedstaticexternboolFindNextUrlCacheGroup(
IntPtrhFind,
reflonglpGroupId,
IntPtrlpReserved);
//ForPInvoke:ReleasesthespecifiedGROUPIDandanyassociatedstateinthecacheindexfile
[DllImport(@"wininet",
SetLastError=true,
CharSet=CharSet.Auto,
EntryPoint="DeleteUrlCacheGroup",
CallingConvention=CallingConvention.StdCall)]
protectedstaticexternboolDeleteUrlCacheGroup(
longGroupId,
intdwFlags,
IntPtrlpReserved);
//ForPInvoke:BeginstheenumerationoftheInternetcache
[DllImport(@"wininet",
SetLastError=true,
CharSet=CharSet.Auto,
EntryPoint="FindFirstUrlCacheEntryA",
CallingConvention=CallingConvention.StdCall)]
protectedstaticexternIntPtrFindFirstUrlCacheEntry(
[MarshalAs(UnmanagedType.LPTStr)]stringlpszUrlSearchPattern,
IntPtrlpFirstCacheEntryInfo,
refintlpdwFirstCacheEntryInfoBufferSize);
//ForPInvoke:RetrievesthenextentryintheInternetcache
[DllImport(@"wininet",
SetLastError=true,
CharSet=CharSet.Auto,
EntryPoint="FindNextUrlCacheEntryA",
CallingConvention=CallingConvention.StdCall)]
protectedstaticexternboolFindNextUrlCacheEntry(
IntPtrhFind,
IntPtrlpNextCacheEntryInfo,
refintlpdwNextCacheEntryInfoBufferSize);
//ForPInvoke:Removesthefilethatisassociatedwiththesourcenamefromthecache,ifthefileexists
[DllImport(@"wininet",
SetLastError=true,
CharSet=CharSet.Auto,
EntryPoint="DeleteUrlCacheEntryA",
CallingConvention=CallingConvention.StdCall)]
protectedstaticexternboolDeleteUrlCacheEntry(
IntPtrlpszUrlName)
publicstaticvoidDelCache(){
//Indicatesthatallofthecachegroupsintheuser'ssystemshouldbeenumerated
constintCACHEGROUP_SEARCH_ALL=0x0;
//Indicatesthatallthecacheentriesthatareassociatedwiththecachegroup
//shouldbedeleted,unlesstheentrybelongstoanothercachegroup.
constintCACHEGROUP_FLAG_FLUSHURL_ONDELETE=0x2;
//Filenotfound.
constintERROR_FILE_NOT_FOUND=0x2;
//Nomoreitemshavebeenfound.
constintERROR_NO_MORE_ITEMS=259;
//PointertoaGROUPIDvariable
longgroupId=0;
//Localvariables
intcacheEntryInfoBufferSizeInitial=0;
intcacheEntryInfoBufferSize=0;
IntPtrcacheEntryInfoBuffer=IntPtr.Zero;
INTERNET_CACHE_ENTRY_INFOAinternetCacheEntry;
IntPtrenumHandle=IntPtr.Zero;
boolreturnValue=false
//Deletethegroupsfirst.
//Groupsmaynotalwaysexistonthesystem.
//Formoreinformation,visitthefollowingMicrosoftWebsite:
//http://msdn.microsoft.com/library/?url=/workshop/networking/wininet/overview/cache.asp
//Bydefault,aURLdoesnotbelongtoanygroup.Therefore,thatcachemaybecome
//emptyevenwhentheCacheGroupAPIsarenotusedbecausetheexistingURLdoesnotbelongtoanygroup.
enumHandle=FindFirstUrlCacheGroup(0,CACHEGROUP_SEARCH_ALL,IntPtr.Zero,0,refgroupId,IntPtr.Zero);
//IftherearenoitemsintheCache,youarefinished.
if(enumHandle!=IntPtr.Zero&&ERROR_NO_MORE_ITEMS==Marshal.GetLastWin32Error())
return;
//LoopthroughCacheGroup,andthendeleteentries.
while(true)
{
//DeleteaparticularCacheGroup.
returnValue=DeleteUrlCacheGroup(groupId,CACHEGROUP_FLAG_FLUSHURL_ONDELETE,IntPtr.Zero);
if(!returnValue&&ERROR_FILE_NOT_FOUND==Marshal.GetLastWin32Error())
{
returnValue=FindNextUrlCacheGroup(enumHandle,refgroupId,IntPtr.Zero);
}
if(!returnValue&&(ERROR_NO_MORE_ITEMS==Marshal.GetLastWin32Error()||ERROR_FILE_NOT_FOUND==Marshal.GetLastWin32Error()))
break;
}
//StarttodeleteURLsthatdonotbelongtoanygroup.
enumHandle=FindFirstUrlCacheEntry(null,IntPtr.Zero,refcacheEntryInfoBufferSizeInitial);
if(enumHandle==IntPtr.Zero&&ERROR_NO_MORE_ITEMS==Marshal.GetLastWin32Error())
return;
cacheEntryInfoBufferSize=cacheEntryInfoBufferSizeInitial;
cacheEntryInfoBuffer=Marshal.AllocHGlobal(cacheEntryInfoBufferSize);
enumHandle=FindFirstUrlCacheEntry(null,cacheEntryInfoBuffer,refcacheEntryInfoBufferSizeInitial);
while(true)
{
internetCacheEntry=(INTERNET_CACHE_ENTRY_INFOA)Marshal.PtrToStructure(cacheEntryInfoBuffer,typeof(INTERNET_CACHE_ENTRY_INFOA));
cacheEntryInfoBufferSizeInitial=cacheEntryInfoBufferSize;
returnValue=DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName);
strings=Marshal.PtrToStringAnsi(internetCacheEntry.lpszLocalFileName);
if(!returnValue)
{
returnValue=FindNextUrlCacheEntry(enumHandle,cacheEntryInfoBuffer,refcacheEntryInfoBufferSizeInitial);
}
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,refcacheEntryInfoBufferSizeInitial);
}
}
Marshal.FreeHGlobal(cacheEntryInfoBuffer);
}
}
}
效果:总体来说还是有点效果的,但是效率极低,会出现长时间的等待情况,程序假死.最重要的是不知道什么时候结束.
③.调用RunDll32.exe
RunCmd("RunDll32.exeInetCpl.cpl,ClearMyTracksByProcess8");
voidRunCmd(stringcmd)
{
System.Diagnostics.Processp=newSystem.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");
}
效果:这个方法解决的我的问题,缓存被清空.
以下是其他一些参数的说明:
//TemporaryInternetFiles(Internet临时文件) //RunDll32.exeInetCpl.cpl,ClearMyTracksByProcess8 //Cookies //RunDll32.exeInetCpl.cpl,ClearMyTracksByProcess2 //History(历史记录) //RunDll32.exeInetCpl.cpl,ClearMyTracksByProcess1 //FormData(表单数据) //RunDll32.exeInetCpl.cpl,ClearMyTracksByProcess16 //Passwords(密码) //RunDll32.exeInetCpl.cpl,ClearMyTracksByProcess32 //DeleteAll(全部删除) //RunDll32.exeInetCpl.cpl,ClearMyTracksByProcess255 //DeleteAll-"Alsodeletefilesandsettingsstoredbyadd-ons" //RunDll32.exeInetCpl.cpl,ClearMyTracksByProcess4351
希望本文所述对大家的C#程序设计有所帮助。