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-2025 CODEPRJ.COM