C#.NET:清除WebBrowser控件缓存文件


一、页面禁止缓存标签(HTML):

1 <head>
2     <meta http-equiv="PRAGMA" content="NO-CACHE" />
3     <meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />
4     <meta http-equiv="EXPIRES" content="0" />
5     <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
6     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

二、URL后添加时间戳(JavaScript):

1 url += url.indexOf("?") > 0 ? "&" : "?";
2 url += "random=" + (new Date()).getTime();

三、URL后添加时间戳(C#):

1 url += url.IndexOf("?") > 0 ? "&" : "?";
2 url += "random=" + DateTime.Now.ToString("yyyyMMddHHmmss");

四、调用ClearMyTracksByProcess(C#):

 1/// <summary>
 2         /// 清除文件夹
 3         /// </summary>
 4         /// <param name="path">文件夹路径</param>
 5         static void FolderClear(string path)
 6         {
 7             System.IO.DirectoryInfo diPath = new System.IO.DirectoryInfo(path);
 8             foreach (System.IO.FileInfo fiCurrFile in diPath.GetFiles())
 9             {
10                 FileDelete(fiCurrFile.FullName);
11 
12             }
13             foreach (System.IO.DirectoryInfo diSubFolder in diPath.GetDirectories())
14             {
15                 FolderClear(diSubFolder.FullName); // Call recursively for all subfolders
16             }
17         }
18         /// <summary>
19         /// 执行命令行
20         /// </summary>
21         /// <param name="cmd"></param>
22         static void RunCmd(string cmd)
23         {
24             ProcessStartInfo p = new ProcessStartInfo();
25             p.FileName = "cmd.exe";
26             p.Arguments = "/c " + cmd;
27             p.WindowStyle = ProcessWindowStyle.Hidden;  // Use a hidden window
28             Process.Start(p);
29         }
30         /// <summary>
31         /// 删除临时文件
32         /// </summary>
33         public static void CleanTempFiles()
34         {
35             FolderClear(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
36             RunCmd("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8");
37         }

此上四法组合起来使用,可有效清除页面缓存。

打赏

免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2022 CODEPRJ.COM