【问题标题】:How to clear browser cache programmatically?如何以编程方式清除浏览器缓存?
【发布时间】:2012-01-08 13:28:07
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

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

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

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

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

    How to delete all files and folders in a directory? 试试看!

    【讨论】:

    • 无法删除此目录,因为使用了其他进程或程序。如何解决..
    • 好吧,你可以删除它,但是你可以通过删除缓存的子目录来清除缓存。
    • 删除缓存的子目录没问题,但是缓存不清楚。注意:一旦firefox关闭,所有文件都会被删除,否则我无法删除它......知道吗?
    • 你的意思是当 Firefox 运行时,它不允许删除子目录,但我试过了,它甚至在 Firefox 运行时也能正常工作。可能您的要求有所不同。
    【解决方案2】:

    出于安全原因,我认为这是不可能的。 最多可以设置 HTTP 标头来告诉浏览器不要像这样访问你的页面:

    Cache-Control: no-cache
    

    【讨论】:

    • 我认为这是您控制网站缓存时的最佳选择。
    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

      使用此代码 (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);
                      });
                  });
              }                    
          }
      

      【讨论】:

        【解决方案5】:

        我的解决方案:

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

        【讨论】:

          【解决方案6】:

          在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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-12-30
            • 1970-01-01
            • 1970-01-01
            • 2014-12-15
            • 2011-09-19
            • 2011-04-16
            • 1970-01-01
            相关资源
            最近更新 更多