如何以编程方式清除浏览器缓存?

我尝试以编程方式清除firefox 8浏览器缓存。 我正在使用asp.net开发网站,出于安全原因,我需要清除浏览器缓存。 我尝试了很多方法来清除缓存,但似乎都没有。 有任何想法吗?

是的,你可以做到但是……..

由于浏览器安全原因,您无法通过代码清除浏览器的历史记录。

但您可以使用文件操作删除浏览器“缓存”目录下的所有文件和文件夹

例如。 Mozilla的默认缓存位置(隐藏)是“..AppData \ Local \ Mozilla \ Firefox \ Profiles \ 2nfq77n2.default \ Cache”

如何删除目录中的所有文件和文件夹? 试试吧!

由于安全原因,我不认为这是可能的。 在max,你可以设置HTTP标头告诉浏览器不要像这样对你的页面进行操作:

Cache-Control: no-cache 

无法以编程方式清除浏览器的缓存,但是您可以停止从应用程序缓存。

下面的代码将帮助您禁用缓存并清除应用程序中的现有缓存:

 public static void DisablePageCaching() { //Used for disabling page caching HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); HttpContext.Current.Response.Cache.SetValidUntilExpires(false); HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); HttpContext.Current.Response.Cache.SetNoStore(); } 

使用此代码(C#):

 public static void DeleteFirefoxCache() { string profilesPath = @"Mozilla\Firefox\Profiles"; string localProfiles = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), profilesPath); string roamingProfiles = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), profilesPath); if (Directory.Exists(localProfiles)) { var profiles = Directory.GetDirectories(localProfiles).OfType().ToList(); profiles.RemoveAll(prfl => prfl.ToLowerInvariant().EndsWith("geolocation")); // do not delete this profile. profiles.ForEach(delegate(string path) { var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).ToList(); foreach (string file in files) { if (!Common.IsFileLocked(new FileInfo(file))) File.Delete(file); } }); } if (Directory.Exists(roamingProfiles)) { var profiles = Directory.GetDirectories(roamingProfiles).OfType().ToList(); profiles.RemoveAll(prfl => prfl.ToLowerInvariant().EndsWith("geolocation")); // do not delete this profile. profiles.ForEach(delegate(string path) { var dirs = Directory.GetDirectories(path, "*", SearchOption.AllDirectories).OfType().ToList(); dirs.ForEach(delegate(string dir) { var files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories).ToList(); foreach (string file in files) { if (!Common.IsFileLocked(new FileInfo(file))) File.Delete(file); } }); var files0 = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly).OfType().ToList(); files0.ForEach(delegate(string file) { if (!Common.IsFileLocked(new FileInfo(file))) File.Delete(file); }); }); } } 

我的解决方案

 string UserProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); try { string id = string.Empty; var lines = File.ReadAllLines($@"{UserProfile}\AppData\Roaming\Mozilla\Firefox\profiles.ini"); foreach (var line in lines) { if (line.Contains("Path=Profiles/")) { var text = line.Replace("Path=Profiles/", ""); id = text.Trim(); } } Array.ForEach(Directory.GetFiles($@"{UserProfile}\AppData\Local\Mozilla\Firefox\Profiles\{id}\cache2\entries"), File.Delete); } catch { }