C# WebBrowser如何清除缓存?

事理 2010-02-26 01:28:30
Microsoft Visual Studio 2005 中的webbrowser浏览器为什么当一个账户登录成功后,却再也不能回到登录页面,总是显示登录成功的页面,除非关掉进程,我反复试过,好像是程序把网页页面缓存住了,请问有没有好的解决办法?最好有代码示例。先谢过了。
...全文
2919 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
HeraclesLiaw 2012-06-21
  • 打赏
  • 举报
回复
调用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
congzhongxiao_57 2011-02-26
  • 打赏
  • 举报
回复
还没呢,我也想知道
NET_Cyberspace 2011-01-13
  • 打赏
  • 举报
回复
我也想知道这个问题,楼主解决了没啊?
事理 2010-10-12
  • 打赏
  • 举报
回复
webbrowser 缓存不能清除。想起他思路吧。
maole413149 2010-10-12
  • 打赏
  • 举报
回复
#10 不行啊
事理 2010-03-08
  • 打赏
  • 举报
回复
C# API实现,不知道行不行,共享一下,找了很久才找到的
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);
}
}
}
事理 2010-02-26
  • 打赏
  • 举报
回复
试过,不行啊。。。。。
kid_wang 2010-02-26
  • 打赏
  • 举报
回复
你清一下Cookie不行吗?在你new一个的同时。
事理 2010-02-26
  • 打赏
  • 举报
回复
怎么注销?我重新new 一个新的webbrowser都不行。
jin20000 2010-02-26
  • 打赏
  • 举报
回复
注销也不错,回复内容太短了!
事理 2010-02-26
  • 打赏
  • 举报
回复
能再详细点吗?这个我看不懂啊。
wuyq11 2010-02-26
  • 打赏
  • 举报
回复
InternetSetCookieA(
IN LPCSTR lpszUrl,
IN LPCSTR lpszCookieName,
IN LPCSTR lpszCookieData
);
事理 2010-02-26
  • 打赏
  • 举报
回复
那怎样是登录信息不写入Cookie呢?
事理 2010-02-26
  • 打赏
  • 举报
回复
但是程序有写入Cookie啊
卧_槽 2010-02-26
  • 打赏
  • 举报
回复
这个,你没把登录信息写入cookice就不会啊。
4 浏览器网络编程(共两周) 4.1 浏览器工作原理(老师讲解1学时,学生理解1学时) 浏览器工作原理如图4-1所示。 图4-1 浏览器工作原理 (1) 浏览器分析超链指向页面的 URL。 (2) 浏览器向 DNS 请求解析 www.tsinghua.edu.cn 的 IP 地址。 (3) 域名系统 DNS 解析出清华大学服务器的 IP 地址。 (4) 浏览器与服务器建立 TCP 连接 (5) 浏览器发出取文件命令: GET /chn/yxsz/index.htm。 (6) 服务器 给出响应,把文件 index.htm 发给浏览器。 (7) TCP 连接释放。 (8) 浏览器显示“清华大学院系设置”文件 index.htm 中的所有文本 4.2 浏览器功能结构(老师讲解1学时,学生理解1学时) 浏览器程序功能结构如图4-2所示。 图4-2 浏览器功能结构 1) 浏览器有一组客户、一组解释程序,以及管理这些客户和解释程序的控制程序。 2) 控制程序是其中的核心部件,它解释鼠标的点击和键盘的输入,并调用有关的组件来执行用户指定的操作。 3) 例如,当用户用鼠标点击一个超链的起点时,控制程序就调用一个客户从所需文档所在的远地服务器上取回该文档,并调用解释程序向用户显示该文档 4) HTML 解释程序是必不可少的,而其他的解释程序则是可选的。 5) 解释程序把 HTML 规格转换为适合用户显示硬件的命令来处理版面的细节。 6) 许多浏览器还包含 FTP 客户,用来获取文件传送服务。 7) 一些浏览器也包含电子邮件客户,使浏览器能够发送和接收电子邮件 8) 浏览器将它取回的每一个页面副本都放入本地磁盘的缓存中。 9) 当用户用鼠标点击某个选项时,浏览器首先检查磁盘的缓存。若缓存中保存了该项,浏览器就直接从缓存中得到该项副本而不必从网络获取,这样就明显地改善浏览器的运行特性。 10) 但缓存要占用磁盘大量的空间,而浏览器性能的改善只有在用户再次查看缓存中的页面时才有帮助。 11) 许多浏览器允许用户调整缓存策略。 4.3 浏览器设计(老师讲解2学时,学生设计2学时) (1)浏览器功能设计 浏览器功能设计主要确定设计好的浏览器需要具备哪些功能,以及这些功能是以什么方式进行实现,这个步骤相当于软件设计中的“需求分析”,其中必须保证基本功能的具备,即浏览器必须能够浏览Web页面。 (2)浏览器界面设计 浏览器界面设计主要确定浏览器的页面布局,设定浏览器窗口上设置哪些功能菜单项、设置哪些功能按钮、设置哪些状态栏,它们各自的位置如何、属性如何、对应的变量如何等等。 (3)浏览器代码设计 浏览器代码设计主要是确定对上述的设计的功能在代码上怎样实现,包括选用的程序语言和编程平台选择、程序代码的编写、程序代码的调试、程序功能的测试等等。 4.4 浏览器编程设计平台(老师讲解2学时,学生理解4学时) (1)VS2005编程平台 (2)WebBrowser类及其使用 (3)一些浏览器程序的代码分析 4.5 浏览器开发步骤(8天) (1)熟悉VS2005平台,建立项目(半天) 打开VS2005选择“创建项目”,进入“新建项目”对话框,如图4-1所示。 图4-1 “新建项目”对话框 在“新建项目”对话框的项目类型窗口中选中“Visual C#”作为项目开发语言,在模板窗口中选中“Windows 应用程序”作为项目开发模板,在“名称”、“位置”编辑框中输入自己设定的项目名字和项目存储位置,本讲义使用“CSharp浏览器”作为项目名称,如图4-2所示。 图4-2 输入新建项目的属性 完成输入内容后,单击“确定”按钮,进入项目VC#开发窗口“Form.cs[设计]”,如图4-3所示。 图4-3 VC#开发窗口“Form.cs[设计]” (2)界面设计(1天) ○1设计菜单栏如图4-4所示。(半天) 图4-4 设计菜单栏 设计“文件”菜单如4-5所示。 图4-5 设计“文件”菜单 设计“编辑”菜单如图4-6所示。 图4-6 设计“编辑”菜单 设计“查看”菜单如图4-7所示。 图4-7 设计“查看”菜单 设计“收藏”菜单如图4-8所示。 图4-8 设计“收藏”菜单 设计“工具”菜单如图4-9所示。 图4-9 设计“工具”菜单 ○2设计工具栏、用户区和状态栏如图4-10所示(半天) 图4-10 工具栏和状态栏 (3)功能代码编辑 ○1初始化代码(半天) 双击“Form”设计页面进入代码编写页面,如图4-11所示。 图4-11 代码编写页面 初始化页面代码如下: ////////////////////////////////////////////////////////

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧