急!需要一个清理C# webbrowser 缓存的类.
-
- 价格:¥150
- 任务编号:28464
- 开始时间:2009-06-30 09:03:41
- 结束时间:2009-07-04 09:03:41
- 任务状态:任务已圆满结束 (1188人关注 14人报名 16个稿件 )
中标稿件:会员gjhjack1980的3371002号稿件 [2010-03-14 07:28:44] 投票时时彩
中标模式单人中标模式,一人独享赏金。
赏金申明本站已经收到客户的汇款,确定此任务总金额的80%可以支付给被客户选中的会员。
版权声明稿件如果被客户选中,在完成赏金支付后,该作品的全部知识产权自动转移给客户;如果没有被选中,可自行处理。
稿件编号:3371002号
- gjhjack1980
- 卖家评价
- 能力:9



- 认证:

- 买家评价
- 信用:0
- 好评:0
- 活跃度
- 权限:
稿件编号:3377709号
发布者已浏览 时间:2009/07/01 10:19:15 评论:0条票数:37 | 分享稿件 | 客户积分兑换礼品
第一个方法是把自己的网页写到一个临时文件,然后navigate到这个文件,Foxmail是这样做的。这样做的缺点是会产生很多临时文件,文件在磁盘上的读写需要耗费较多时间,而且要记得及时清理。
第二个方法是用COM中IPersistStreamInit之类的,使用流操作,据说Outlook便是这样做的。这样显然是最好的,在内存中形成网页进行操作速度也很快。但我在.Net里找不到这个接口,所以不知道如何实现。应用程序操作浏览器/网页: WebBrowser浏览器控件提供了一些函数,如ExecWB,可以使浏览器执行内部定义的一些操作,如执行另存为: webBrowser.ExecWB( SHDocVw.OLECMDID.OLECMDID_SAVEAS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref objNull, ref objNull ); 但似乎这里按取消的话会抛出一个异常,所以我用一个空的try{}catch(Exception){}来捕获它,就不会有错误了。 还可以用DOM里的一些方法来操作网页,举个例子,可以这样调用网页中写好的一个Javascript函数func(): ((mshtml.HTMLDocumentClass)webBrowser.Document).parentWindow.execScript( "func()", "JScript" );
第二个方法是用COM中IPersistStreamInit之类的,使用流操作,据说Outlook便是这样做的。这样显然是最好的,在内存中形成网页进行操作速度也很快。但我在.Net里找不到这个接口,所以不知道如何实现。应用程序操作浏览器/网页: WebBrowser浏览器控件提供了一些函数,如ExecWB,可以使浏览器执行内部定义的一些操作,如执行另存为: webBrowser.ExecWB( SHDocVw.OLECMDID.OLECMDID_SAVEAS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref objNull, ref objNull ); 但似乎这里按取消的话会抛出一个异常,所以我用一个空的try{}catch(Exception){}来捕获它,就不会有错误了。 还可以用DOM里的一些方法来操作网页,举个例子,可以这样调用网页中写好的一个Javascript函数func(): ((mshtml.HTMLDocumentClass)webBrowser.Document).parentWindow.execScript( "func()", "JScript" );
稿件编号:3380525号
发布者已浏览 时间:2009/07/01 17:01:47 评论:0条票数:81 | 分享稿件 | 客户积分兑换礼品
下载附件
*************************
说明
*************************
由于缓存文件是特殊的文件,以及WebBrowser与IE版本有关
因此删除缓存绝对不可能用一些IO函数就总可以解决的
因此我的这些函数在IO操作的基础上,又针对环境进行相应的清理。
static class WebCleaner
{
/*
* 7 个静态函数
* 私有函数
* private bool FileDelete() : 删除文件
* private void FolderClear() : 清除文件夹内的所有文件
* private void RunCmd() : 运行内部命令
*
* 公有函数
* public void CleanCookie() : 删除Cookie
* public void CleanHistory() : 删除历史记录
* public void CleanTempFiles() : 删除临时文件
* public void CleanAll() : 删除所有
*
*
*
* */
//private
///
/// 删除一个文件,System.IO.File.Delete()函数不可以删除只读文件,这个函数可以强行把只读文件删除。
///
/// 文件路径
/// 是否被删除
static bool FileDelete(string path)
{
//first set the File's ReadOnly to 0
//if EXP, restore its Attributes
System.IO.FileInfo file = new System.IO.FileInfo(path);
System.IO.FileAttributes att = 0;
bool attModified = false;
try
{
//### ATT_GETnSET
att = file.Attributes;
file.Attributes &= (~System.IO.FileAttributes.ReadOnly);
attModified = true;
file.Delete();
}
catch (Exception e)
{
if (attModified)
file.Attributes = att;
return false;
}
return true;
}
//public
///
/// 清除文件夹
///
/// 文件夹路径
static void FolderClear(string path)
{
System.IO.DirectoryInfo diPath = new System.IO.DirectoryInfo(path);
foreach (System.IO.FileInfo fiCurrFile in diPath.GetFiles())
{
FileDelete(fiCurrFile.FullName);
}
foreach (System.IO.DirectoryInfo diSubFolder in diPath.GetDirectories())
{
FolderClear(diSubFolder.FullName); // Call recursively for all subfolders
}
}
static void RunCmd(string cmd)
{
System.Diagnostics.Process.Start("cmd.exe", "/c " + cmd);
}
///
/// 删除历史记录
///
public static void CleanHistory()
{
string[] theFiles = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.History), "*", System.IO.SearchOption.AllDirectories);
foreach (string s in theFiles)
FileDelete(s);
RunCmd("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1");
}
///
/// 删除临时文件
///
public static void CleanTempFiles()
{
FolderClear(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
RunCmd("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8");
}
///
/// 删除Cookie
///
public static void CleanCookie()
{
string[] theFiles = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies), "*", System.IO.SearchOption.AllDirectories);
foreach (string s in theFiles)
FileDelete(s);
RunCmd("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2");
}
///
/// 删除全部
///
public static void CleanAll()
{
CleanHistory();
CleanCookie();
CleanTempFiles();
}
}
下载附件 *************************
说明
*************************
由于缓存文件是特殊的文件,以及WebBrowser与IE版本有关
因此删除缓存绝对不可能用一些IO函数就总可以解决的
因此我的这些函数在IO操作的基础上,又针对环境进行相应的清理。
static class WebCleaner
{
/*
* 7 个静态函数
* 私有函数
* private bool FileDelete() : 删除文件
* private void FolderClear() : 清除文件夹内的所有文件
* private void RunCmd() : 运行内部命令
*
* 公有函数
* public void CleanCookie() : 删除Cookie
* public void CleanHistory() : 删除历史记录
* public void CleanTempFiles() : 删除临时文件
* public void CleanAll() : 删除所有
*
*
*
* */
//private
///
/// 删除一个文件,System.IO.File.Delete()函数不可以删除只读文件,这个函数可以强行把只读文件删除。
///
/// 文件路径
/// 是否被删除
static bool FileDelete(string path)
{
//first set the File's ReadOnly to 0
//if EXP, restore its Attributes
System.IO.FileInfo file = new System.IO.FileInfo(path);
System.IO.FileAttributes att = 0;
bool attModified = false;
try
{
//### ATT_GETnSET
att = file.Attributes;
file.Attributes &= (~System.IO.FileAttributes.ReadOnly);
attModified = true;
file.Delete();
}
catch (Exception e)
{
if (attModified)
file.Attributes = att;
return false;
}
return true;
}
//public
///
/// 清除文件夹
///
/// 文件夹路径
static void FolderClear(string path)
{
System.IO.DirectoryInfo diPath = new System.IO.DirectoryInfo(path);
foreach (System.IO.FileInfo fiCurrFile in diPath.GetFiles())
{
FileDelete(fiCurrFile.FullName);
}
foreach (System.IO.DirectoryInfo diSubFolder in diPath.GetDirectories())
{
FolderClear(diSubFolder.FullName); // Call recursively for all subfolders
}
}
static void RunCmd(string cmd)
{
System.Diagnostics.Process.Start("cmd.exe", "/c " + cmd);
}
///
/// 删除历史记录
///
public static void CleanHistory()
{
string[] theFiles = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.History), "*", System.IO.SearchOption.AllDirectories);
foreach (string s in theFiles)
FileDelete(s);
RunCmd("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1");
}
///
/// 删除临时文件
///
public static void CleanTempFiles()
{
FolderClear(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
RunCmd("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8");
}
///
/// 删除Cookie
///
public static void CleanCookie()
{
string[] theFiles = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies), "*", System.IO.SearchOption.AllDirectories);
foreach (string s in theFiles)
FileDelete(s);
RunCmd("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2");
}
///
/// 删除全部
///
public static void CleanAll()
{
CleanHistory();
CleanCookie();
CleanTempFiles();
}
}
稿件编号:3381204号
稿件编号:3390516号
稿件编号:3392040号
- maidianshop
- 卖家评价
- 能力:14




- 认证:

- 买家评价
- 信用:0
- 好评:0
- 活跃度
- 权限:
稿件编号:3379839号
- tyougyokuki
- 卖家评价
- 能力:1

- 认证:

- 买家评价
- 信用:0
- 好评:0
- 活跃度
- 权限:
稿件编号:3370890号
发布者已浏览 时间:2009/06/30 09:26:48 评论:0条票数:0 | 分享稿件 | 客户积分兑换礼品
要在 Visual C#.NET 中,使用 WinInet 函数清除所有缓存中文件
using System;
using System.Runtime.InteropServices;
// Visual C# version of Q326201
namespace Q326201CS
{
// Class for deleting the cache.
class DeleteCache
{
// For PInvoke: Contains information about an entry in the Internet cache
[StructLayout(LayoutKind.Explicit, Size=80)]
public 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)]
public 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)]
public 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)]
public 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)]
public 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)]
public 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)]
public static extern bool DeleteUrlCacheEntry(
IntPtr lpszUrlName);
[STAThread]
static void Main(string[] args)
{
// 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);
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);
}
}
}
using System;
using System.Runtime.InteropServices;
// Visual C# version of Q326201
namespace Q326201CS
{
// Class for deleting the cache.
class DeleteCache
{
// For PInvoke: Contains information about an entry in the Internet cache
[StructLayout(LayoutKind.Explicit, Size=80)]
public 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)]
public 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)]
public 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)]
public 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)]
public 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)]
public 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)]
public static extern bool DeleteUrlCacheEntry(
IntPtr lpszUrlName);
[STAThread]
static void Main(string[] args)
{
// 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);
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);
}
}
}
稿件编号:3370919号
稿件编号:3370948号
- gjhjack1980
- 卖家评价
- 能力:9



- 认证:

- 买家评价
- 信用:0
- 好评:0
- 活跃度
- 权限:
稿件编号:3370977号
稿件编号:3370983号
发布者已浏览 时间:2009/06/30 09:41:10 评论:0条票数:0 | 分享稿件 | 客户积分兑换礼品
WinInet 函数
要清除缓存直接,您可以使用以下 WinInet 函数:
使用 FindFirstURLCacheEntry 函数以查找第一个缓存条目。
使用 FindNextUrlCacheEntry 函数来枚举缓存。
使用 DeleteUrlCacheEntry 函数删除每个项。
本文中的此代码示例使用所有这些功能。
注意 : 这些函数仅可用于 Microsoft Internet Explorer 5。 因此,必须包含适当检查本文中的代码示例包括) 以防止出现错误。
回到顶端
清除缓存 Visual C#.NET 中的步骤
要在 Visual C#.NET 中,使用 WinInet 函数清除所有缓存中文件,请按照下列步骤:
启动 Microsoft Visual Studio.NET。
在 文件 菜单上指向 新建 ,然后单击 项目 。
在在 新建项目 对话框中单击 项目类型 下的 Visual C# 项目 ,然后单击 模板 下的 控制台应用程序 。
将以下代码添加到 Class 1 类:
using System;
using System.Runtime.InteropServices;
// Visual C# version of Q326201
namespace Q326201CS
{
// Class for deleting the cache.
class DeleteCache
{
// For PInvoke: Contains information about an entry in the Internet cache
[StructLayout(LayoutKind.Explicit, Size=80)]
public 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)]
public 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)]
public 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)]
public 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)]
public 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)]
public 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)]
public static extern bool DeleteUrlCacheEntry(
IntPtr lpszUrlName);
[STAThread]
static void Main(string[] args)
{
// 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);
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);
}
}
}
编译,然后运行该项目。
若要确认 Internet 临时文件缓存中的已被删除,请按照下列 Microsoft Internet Explorer 中的步骤操作:
在 工具 菜单上单击 Internet 选项 。
在在 常规 选项卡上 临时 Internet 文件 区域中,单击 设置 。
单击 查看文件 。 请注意已删除的所有文件在 Internet Explorer 缓存中。
要清除缓存直接,您可以使用以下 WinInet 函数:
使用 FindFirstURLCacheEntry 函数以查找第一个缓存条目。
使用 FindNextUrlCacheEntry 函数来枚举缓存。
使用 DeleteUrlCacheEntry 函数删除每个项。
本文中的此代码示例使用所有这些功能。
注意 : 这些函数仅可用于 Microsoft Internet Explorer 5。 因此,必须包含适当检查本文中的代码示例包括) 以防止出现错误。
回到顶端
清除缓存 Visual C#.NET 中的步骤
要在 Visual C#.NET 中,使用 WinInet 函数清除所有缓存中文件,请按照下列步骤:
启动 Microsoft Visual Studio.NET。
在 文件 菜单上指向 新建 ,然后单击 项目 。
在在 新建项目 对话框中单击 项目类型 下的 Visual C# 项目 ,然后单击 模板 下的 控制台应用程序 。
将以下代码添加到 Class 1 类:
using System;
using System.Runtime.InteropServices;
// Visual C# version of Q326201
namespace Q326201CS
{
// Class for deleting the cache.
class DeleteCache
{
// For PInvoke: Contains information about an entry in the Internet cache
[StructLayout(LayoutKind.Explicit, Size=80)]
public 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)]
public 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)]
public 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)]
public 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)]
public 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)]
public 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)]
public static extern bool DeleteUrlCacheEntry(
IntPtr lpszUrlName);
[STAThread]
static void Main(string[] args)
{
// 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);
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);
}
}
}
编译,然后运行该项目。
若要确认 Internet 临时文件缓存中的已被删除,请按照下列 Microsoft Internet Explorer 中的步骤操作:
在 工具 菜单上单击 Internet 选项 。
在在 常规 选项卡上 临时 Internet 文件 区域中,单击 设置 。
单击 查看文件 。 请注意已删除的所有文件在 Internet Explorer 缓存中。
稿件编号:3371026号
稿件编号:3374186号
- qq397640359
- 卖家评价
- 能力:0
- 认证:

- 买家评价
- 信用:0
- 好评:0
- 活跃度
- 权限:
稿件编号:3376032号
- zhanghairong
- 卖家评价
- 能力:1

- 认证:

- 买家评价
- 信用:0
- 好评:0
- 活跃度
- 权限:

