c# webbrowser 清除cookie和缓存
2014-06-08 12:13
344 查看
由于缓存文件是特殊的文件,以及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(); } } 这样应该可以删。
相关文章推荐
- C#清除WebBrowser中Cookie缓存的方法
- 清除webBrowser 缓存和Cookie的解决方案
- C#.NET:清除WebBrowser控件缓存文件
- 清除webBrowser 缓存和Cookie的解决方案
- 清除webBrowser 缓存和Cookie的解决方案
- 清除webBrowser 缓存和Cookie的解决方案
- 清除webBrowser 缓存和Cookie的解决方案
- 【.Net码农】清除webBrowser 缓存和Cookie的解决方案
- C# 通过调用Win32 API函数清除浏览器缓存和cookie
- .net C# cookie清除问题
- C#清除页面缓存
- 如何在C# WinForm 程序中使用WebBrowser控件时设置COOKIE的值。
- c# 清除IE记录 历史记录 临时文件 Cookie
- Java Cookie 浏览器缓存清除方案!
- UIWebView清除缓存和cookie
- UIWebView清除缓存和cookie
- C# ASP.NET操作模态窗口时清除缓存
- C#利用WebBrowser获取完整COOKIE介绍
- C#页面清除缓存
- C#利用WebBrowser获取完整COOKIE介绍 (转帖)