问题描述
程序的目的是删除www.apple.com网站的cookie,核心代码如下:DimsAsString=""DimnNeededAsInteger=0,nBufSizeAsIntegerDimbufAsIntPtrDimhEntryAsIntPtrDimCacheItemAsINTERNET_CACHE_ENTRY_INFOhEntry=FindFirstUrlCacheEntry(Nothing,IntPtr.Zero,nNeeded)nBufSize=nNeededbuf=Marshal.AllocHGlobal(nBufSize)hEntry=FindFirstUrlCacheEntry(Nothing,buf,nNeeded)IfMarshal.GetLastWin32Error()=ERROR_NO_MORE_ITEMSThenReturn""EndIfWhileMarshal.GetLastWin32Error()<>ERROR_NO_MORE_ITEMSDimreturnvalueAsBooleanTryCacheItem=DirectCast(Marshal.PtrToStructure(buf,GetType(INTERNET_CACHE_ENTRY_INFO)),INTERNET_CACHE_ENTRY_INFO)IfMarshal.PtrToStringAuto(CacheItem.lpszSourceUrlName).IndexOf("Cookie")>=0AndMarshal.PtrToStringAuto(CacheItem.lpszSourceUrlName).IndexOf("@apple.com")>=0Thenreturnvalue=DeleteUrlCacheEntry(CacheItem.lpszSourceUrlName)Ifreturnvalue=falsethenMsgBox(Marshal.GetLastWin32Error())EndIFEndIfreturnvalue=FindNextUrlCacheEntry(hEntry,buf,4096)CatchexAsSystem.ExceptionConsole.WriteLine(ex.Message+ex.StackTrace)EndTryEndWhileMarshal.FreeHGlobal(buf)Returns
目前问题出在”DeleteUrlCacheEntry(CacheItem.lpszSourceUrlName)"上,经过测试,该函数返回false我用getlastwin32error,发现错误信息是“找不到文件”我检查过了,CacheItem的各个参数都正常,调试时也确实是我要删除的文件,为什么会出现这样奇怪的错误呢?备注:1CacheItem.lpszSourceUrlName是一个intptr指针,不是字符串2FindNextUrlCacheEntry(hEntry,buf,4096)中用4096以防止因空间不足而引起的获取CacheEntry失败
解决方案
解决方案二:
BOOLDeleteUrlCacheEntry(LPCTSTRlpszUrlName);函数功能删除Cache中指定的源文件名,如果指定文件存在则删除参数说明lpszUrlName源文件的名字该名字不能包含换码符返回值如果成功返回真否则返回假
解决方案三:
代码整全点咱也好帮你测试呀。除Cookie外的能删除吗?看一下这个,以前收集的但没仔细研究过:http://vbnet.mvps.org/index.html?code/internet/deleteurlcache.htm
解决方案四:
把全部代码贴上给大家看看ImportsSystemImportsSystem.Runtime.InteropServicesPublicClassGetCache#Region"引入所需要的Dll"<StructLayout(LayoutKind.Sequential,CharSet:=CharSet.Auto)>_PrivateStructureINTERNET_CACHE_ENTRY_INFOPublicdwStructSizeAsIntegerPubliclpszSourceUrlNameAsIntPtrPubliclpszLocalFileNameAsIntPtrPublicCacheEntryTypeAsIntegerPublicdwUseCountAsIntegerPublicdwHitRateAsIntegerPublicdwSizeLowAsIntegerPublicdwSizeHighAsIntegerPublicLastModifiedTimeAsComTypes.FILETIMEPublicExpireTimeAsComTypes.FILETIMEPublicLastAccessTimeAsComTypes.FILETIMEPublicLastSyncTimeAsComTypes.FILETIMEPubliclpHeaderInfoAsIntPtrPublicdwHeaderInfoSizeAsIntegerPubliclpszFileExtensionAsIntPtrPublicdwExemptDeltaAsIntegerEndStructure<StructLayout(LayoutKind.Sequential,CharSet:=CharSet.Auto)>_PrivateStructureSYSTEMTIMEPublicwYearAsShortPublicwMonthAsShortPublicwDayOfWeekAsShortPublicwDayAsShortPublicwHourAsShortPublicwMinuteAsShortPublicwSecondAsShortPublicwMillisecondsAsShortEndStructure<DllImport("kernel32.dll",SetLastError:=True,CharSet:=CharSet.Auto)>_PrivateSharedFunctionFileTimeToSystemTime(ByVallpFileTimeAsIntPtr,ByVallpSystemTimeAsIntPtr)AsIntegerEndFunction<DllImport("wininet",SetLastError:=True,CharSet:=CharSet.Auto,EntryPoint:="FindFirstUrlCacheGroup",CallingConvention:=CallingConvention.StdCall)>_PrivateSharedFunctionFindFirstUrlCacheGroup(ByValdwFlagsAsInteger,ByValdwFilterAsInteger,ByVallpSearchConditionAsIntPtr,ByValdwSearchConditionAsInteger,ByReflpGroupIdAsLong,ByVallpReservedAsIntPtr)AsIntPtrEndFunction<DllImport("wininet",SetLastError:=True,CharSet:=CharSet.Auto,EntryPoint:="FindNextUrlCacheGroup",CallingConvention:=CallingConvention.StdCall)>_PrivateSharedFunctionFindNextUrlCacheGroup(ByValhFindAsIntPtr,ByReflpGroupIdAsLong,ByVallpReservedAsIntPtr)AsBooleanEndFunction<DllImport("wininet",SetLastError:=True,CharSet:=CharSet.Auto,EntryPoint:="DeleteUrlCacheGroup",CallingConvention:=CallingConvention.StdCall)>_PrivateSharedFunctionDeleteUrlCacheGroup(ByValGroupIdAsLong,ByValdwFlagsAsInteger,ByVallpReservedAsIntPtr)AsBooleanEndFunction<DllImport("wininet.dll",SetLastError:=True,CharSet:=CharSet.Auto)>_PrivateSharedFunctionFindFirstUrlCacheEntry(<MarshalAs(UnmanagedType.LPTStr)>ByValUrlSearchPatternAsString,ByVallpFirstCacheEntryInfoAsIntPtr,ByReflpdwFirstCacheEntryInfoBufferSizeAsInteger)AsIntPtrEndFunction<DllImport("wininet.dll",SetLastError:=True,CharSet:=CharSet.Auto)>_PrivateSharedFunctionFindNextUrlCacheEntry(ByValhEnumHandleAsIntPtr,ByVallpNextCacheEntryInfoAsIntPtr,ByReflpdwNextCacheEntryInfoBufferSizeAsInteger)AsBooleanEndFunction<DllImport("wininet.dll",SetLastError:=True,CharSet:=CharSet.Auto)>_PrivateSharedFunctionGetUrlCacheEntryInfo(<MarshalAs(UnmanagedType.LPTStr)>ByVallpszUrlNameAsString,ByVallpCacheEntryInfoAsIntPtr,ByReflpdwCacheEntryInfoBufferSizeAsInteger)AsBooleanEndFunction<DllImport("wininet.dll")>_PrivateSharedFunctionFindCloseUrlCache(ByValhEnumHandleAsIntPtr)AsBooleanEndFunction<DllImport("wininet",SetLastError:=True,CharSet:=CharSet.Auto,EntryPoint:="DeleteUrlCacheEntryA",CallingConvention:=CallingConvention.StdCall)>_PrivateSharedFunctionDeleteUrlCacheEntry(ByVallpszUrlNameAsString)AsBooleanEndFunctionConstERROR_ACCESS_DENIEDAsInteger=8ConstERROR_NO_MORE_ITEMSAsInteger=259ConstCACHEGROUP_ATTRIBUTE_BASICAsInteger=&H1ConstCACHEGROUP_ATTRIBUTE_FLAGAsInteger=&H2ConstCACHEGROUP_ATTRIBUTE_GET_ALLAsInteger=&HFFFFFFFFConstCACHEGROUP_ATTRIBUTE_GROUPNAMEAsInteger=&H10ConstCACHEGROUP_ATTRIBUTE_QUOTAAsInteger=&H8ConstCACHEGROUP_ATTRIBUTE_STORAGEAsInteger=&H20ConstCACHEGROUP_ATTRIBUTE_TYPEAsInteger=&H4ConstCACHEGROUP_FLAG_FLUSHURL_ONDELETEAsInteger=&H2ConstCACHEGROUP_FLAG_GIDONLYAsInteger=&H4ConstCACHEGROUP_FLAG_NONPURGEABLEAsInteger=&H1ConstCACHEGROUP_READWRITE_MASKAsInteger=&H3CConstCACHEGROUP_SEARCH_ALLAsInteger=&H0ConstCACHEGROUP_SEARCH_BYURLAsInteger=&H1ConstCACHEGROUP_TYPE_INVALIDAsInteger=&H1ConstGROUP_OWNER_STORAGE_SIZEAsInteger=&H4ConstGROUPNAME_MAX_LENGTHAsInteger=&H78#EndRegion#Region"加载数据"''''''获得指定的URL的cache文件名''''''URL'''如果不存在该cache,返回空字符串PublicFunctionGetURLCacheFileName(ByValURLAsString)AsStringDimsAsString=""DimnNeededAsInteger=0,nBufSizeAsIntegerDimbufAsIntPtrDimhEntryAsIntPtrDimCacheItemAsINTERNET_CACHE_ENTRY_INFOhEntry=FindFirstUrlCacheEntry(Nothing,IntPtr.Zero,nNeeded)nBufSize=nNeededbuf=Marshal.AllocHGlobal(nBufSize)hEntry=FindFirstUrlCacheEntry(Nothing,buf,nNeeded)IfMarshal.GetLastWin32Error()=ERROR_NO_MORE_ITEMSThenReturn""EndIfWhileMarshal.GetLastWin32Error()<>ERROR_NO_MORE_ITEMSDimreturnvalueAsBooleanDimbufferlengthAsIntegerTryCacheItem=DirectCast(Marshal.PtrToStructure(buf,GetType(INTERNET_CACHE_ENTRY_INFO)),INTERNET_CACHE_ENTRY_INFO)IfMarshal.PtrToStringAuto(CacheItem.lpszSourceUrlName).IndexOf("Cookie")>=0AndMarshal.PtrToStringAuto(CacheItem.lpszSourceUrlName).IndexOf("@apple.com")>=0Thenreturnvalue=DeleteUrlCacheEntry(Marshal.PtrToStringAuto(CacheItem.lpszSourceUrlName))Ifreturnvalue=FalseThenMsgBox(Marshal.GetLastWin32Error())'此处一直打印出代码为2的系统错误(即找不到文件路径)EndIfEndIfFindNextUrlCacheEntry(hEntry,0,bufferlength)'先获得bufferlength,再读取一次returnvalue=FindNextUrlCacheEntry(hEntry,buf,bufferlength)CatchexAsSystem.ExceptionConsole.WriteLine(ex.Message+ex.StackTrace)EndTryEndWhileMarshal.FreeHGlobal(buf)ReturnsEndFunction#EndRegionEndClass
解决方案五:
我函数名没有改,GetURLCacheFileName就是为了执行删除@apple.com的操作2楼的兄台给的VB6代码我放到.NET上试验过了。还是不行。不仅仅是COOKIE,其他脱机文件也不能删除
解决方案六:
有兄弟关注下么
解决方案七:
不好意思,最近比较忙,才来看这个帖子。你好像走了弯路,看看我删除缓存文件的方法:vb6:OptionExplicitPrivateDeclareFunctionGetUrlCacheEntryInfoLib"wininet.dll"Alias"GetUrlCacheEntryInfoA"(ByValsUrlNameAsString,lpCacheEntryInfoAsAny,lpdwCacheEntryInfoBufferSizeAsLong)AsLongPrivateDeclareFunctionDeleteUrlCacheEntryLib"wininet.dll"Alias"DeleteUrlCacheEntryA"(ByVallpszUrlNameAsString)AsLongPublicFunctionInCache(ByValURLAsString)AsBooleanIfGetUrlCacheEntryInfo(URL,ByVal0&,0)=0ThenInCache=(Err.LastDllError=122)EndIfEndFunctionPublicSubDeleteCacheURL(ByValURLAsString)DeleteUrlCacheEntryURLEndSubPrivateSubCommand1_Click()DimURLAsStringURL="http://community.csdn.net/HomePage.aspx"If(InCache(URL))ThenDeleteCacheURLURLEndIfEndSub
VB.NET:PrivateDeclareFunctionGetUrlCacheEntryInfoLib"wininet.dll"Alias"GetUrlCacheEntryInfoA"(ByValsUrlNameAsString,ByReflpCacheEntryInfoAsIntPtr,ByReflpdwCacheEntryInfoBufferSizeAsIntPtr)AsIntegerPrivateDeclareFunctionDeleteUrlCacheEntryLib"wininet.dll"Alias"DeleteUrlCacheEntryA"(ByVallpszUrlNameAsString)AsIntegerPublicFunctionInCache(ByValURLAsString)AsBooleanIfGetUrlCacheEntryInfo(URL,0&,0)=0ThenInCache=(Err.LastDllError=122)EndIfEndFunctionPublicSubDeleteCacheURL(ByValURLAsString)DeleteUrlCacheEntry(URL)EndSubPrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.ClickDimURLAsStringURL="http://community.csdn.net/HomePage.aspx"If(InCache(URL))ThenDeleteCacheURL(URL)EndIfEndSub