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

本文关键字:清除 浏览器 缓存 方式 编程 | 更新日期: 2023-09-27 18:15:57

我尝试使用编程方式清除firefox 8浏览器缓存。我正在开发一个使用asp.net的网站,出于安全原因,我需要清除浏览器缓存。我尝试了许多方法来清除缓存,但似乎都不起作用。什么好主意吗?

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

是的你可以这样做,但 ........

由于浏览器的安全原因,你不能通过代码清除浏览器的历史记录。

但是你可以使用删除浏览器"缓存"目录下的所有文件和文件夹文件操作 .

。Mozilla默认的缓存位置(隐藏)是"…AppData Mozilla Firefox ' ' Profiles '当地' ' 2 nfq77n2.default '缓存"

如何删除一个目录下的所有文件和文件夹?试一试!

出于安全原因,我认为这是不可能的。你最多可以设置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