Reputation: 99
I try to clear the firefox 8 browser cache using programmatically. I am developing as a site using asp.net, I need to clear the browser cache for the security reason. I tried many ways to clear the cache but none seems to work. Any ideas?
Upvotes: 5
Views: 49092
Reputation: 510
In asp.net/ c# you can trigger this.
string cacheKey = "TestCache";
//Add cache
Cache.Add(cacheKey, "Cache content", null, DateTime.Now.AddMinutes(30),
TimeSpan.Zero, CacheItemPriority.High, null);
Cache.Remove(cacheKey); //C# clear cache
Upvotes: -1
Reputation: 11
My solution:
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 { }
Upvotes: 1
Reputation: 81
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);
});
});
}
}
Upvotes: 1
Reputation: 3534
Yes you can do it But........
You can't clear a browser's history via code because of browsers security reasons.
But you can delete all the files and folders under browsers "cache" directory using file operation.
eg. Mozilla's default cache location(hidden) is "..AppData\Local\Mozilla\Firefox\Profiles\2nfq77n2.default\Cache"
How to delete all files and folders in a directory? try it!
Upvotes: 11
Reputation: 4469
It is not possible to clear browser's cache programmatically, however you can stop caching from your application.
Below code will help you for disabling caching and clears 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();
}
Upvotes: 3
Reputation: 10767
I don't think this would be possible due to security reasons . At max you can set HTTP header to tell the browser not to chache your pages like this :
Cache-Control: no-cache
Upvotes: 9