【发布时间】:2012-01-08 13:28:07
【问题描述】:
我尝试以编程方式清除 firefox 8 浏览器缓存。我正在使用 asp.net 作为站点进行开发,出于安全原因,我需要清除浏览器缓存。我尝试了很多方法来清除缓存,但似乎都没有奏效。有什么想法吗?
【问题讨论】:
我尝试以编程方式清除 firefox 8 浏览器缓存。我正在使用 asp.net 作为站点进行开发,出于安全原因,我需要清除浏览器缓存。我尝试了很多方法来清除缓存,但似乎都没有奏效。有什么想法吗?
【问题讨论】:
是的,你可以做到但是........
出于浏览器安全原因,您无法通过代码清除浏览器的历史记录。
但是您可以删除浏览器“缓存”目录下的所有文件和文件夹,使用 文件操作.
例如。 Mozilla 的默认缓存位置(隐藏)是 "..AppData\Local\Mozilla\Firefox\Profiles\2nfq77n2.default\Cache"
【讨论】:
出于安全原因,我认为这是不可能的。 最多可以设置 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<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);
});
});
}
}
【讨论】:
我的解决方案:
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 { }
【讨论】:
在asp.net/c#中可以触发这个。
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
【讨论】: