How to clear browser cache programmatically?

I am trying to clear the browser cache of Firefox 8 programmatically. I am developing as a site using asp.net, I need to clear my browser cache for security reasons. I tried many ways to clear the cache, but no one works. Any ideas?

+4
source share
5 answers

Yes, you can do it, but ........

You cannot clear your browsing history using code due to browser security considerations.

But you can delete all files and folders in the browsers caching directory using the file .

eg. Mozilla cache location (hidden) "..AppData \ Local \ Mozilla \ Firefox \ Profiles \ 2nfq77n2.default \ Cache"

How to delete all files and folders in a directory? try it!

+9
source

I do not think this would be possible due to security concerns. In max, you can configure the HTTP header to tell the browser not to read your pages as follows:

Cache-Control: no-cache 
+7
source

It is not possible to clear the browser cache programmatically, however you can stop caching from your application.

The code below will help you disable caching and clear the existing cache from your application:

 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(); } 
+2
source

Use this code (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<string>().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<string>(); foreach (string file in files) { if (!Common.IsFileLocked(new FileInfo(file))) File.Delete(file); } }); } if (Directory.Exists(roamingProfiles)) { var profiles = Directory.GetDirectories(roamingProfiles).OfType<string>().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<string>().ToList(); dirs.ForEach(delegate(string dir) { var files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories).ToList<string>(); foreach (string file in files) { if (!Common.IsFileLocked(new FileInfo(file))) File.Delete(file); } }); var files0 = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly).OfType<string>().ToList(); files0.ForEach(delegate(string file) { if (!Common.IsFileLocked(new FileInfo(file))) File.Delete(file); }); }); } } 
0
source

My decision:

 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 { } 
0
source

Source: https://habr.com/ru/post/1383569/


All Articles