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

C# 如何以编程方式清除浏览器缓存?,c#,asp.net,C#,Asp.net,我尝试以编程方式清除Firefox8浏览器缓存。我正在开发一个使用asp.net的网站,出于安全原因,我需要清除浏览器缓存。我尝试了很多方法来清除缓存,但似乎都不起作用。有什么想法吗?由于安全原因,我认为这是不可能的。 在最大值下,您可以设置HTTP标头,以告知浏览器不要像以下方式更改页面: Cache-Control: no-cache 无法通过编程方式清除浏览器的缓存,但可以停止应用程序中的缓存 以下代码将帮助您禁用缓存并从应用程序中清除现有缓存: public static void D

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

由于安全原因,我认为这是不可能的。 在最大值下,您可以设置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();
} 

是的,你可以做,但是

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

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

Mozilla的默认缓存位置(隐藏)为 “.AppData\Local\Mozilla\Firefox\Profiles\2nfq77n2.default\Cache”

试试看

使用以下代码(C#):

公共静态void DeleteFirefoxCache()
{
字符串profilesPath=@“Mozilla\Firefox\Profiles”;
字符串localProfiles=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),profilesPath);
字符串roamingProfiles=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),profilesPath);
if(Directory.Exists(localProfiles))
{
var profiles=Directory.GetDirectories(localProfiles).OfType().ToList();
profiles.RemoveAll(prfl=>prfl.ToLowerInvariant().EndsWith(“地理位置”);//不要删除此配置文件。
profiles.ForEach(委托(字符串路径)
{                    
var files=Directory.GetFiles(路径“**”,SearchOption.AllDirectories.ToList();
foreach(文件中的字符串文件)
{
如果(!Common.IsFileLocked(新文件信息(文件)))
文件。删除(文件);
}            
});
}
if(Directory.Exists(roamingProfiles))
{
var profiles=Directory.GetDirectories(roamingProfiles.OfType().ToList();
profiles.RemoveAll(prfl=>prfl.ToLowerInvariant().EndsWith(“地理位置”);//不要删除此配置文件。
profiles.ForEach(委托(字符串路径)
{
var dirs=Directory.GetDirectories(路径“*”,SearchOption.AllDirectories).OfType().ToList();
目录ForEach(委托(字符串目录)
{
var files=Directory.GetFiles(dir,“**”,SearchOption.AllDirectories.ToList();
foreach(文件中的字符串文件)
{
如果(!Common.IsFileLocked(新文件信息(文件)))
文件。删除(文件);
}   
});
var files0=Directory.GetFiles(路径“*”,SearchOption.TopDirectoryOnly).OfType().ToList();
files0.ForEach(委托(字符串文件)
{
如果(!Common.IsFileLocked(新文件信息(文件)))
文件。删除(文件);
});
});
}                    
}
我的解决方案:

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

无法删除此目录,因为正在使用另一个进程或程序。如何解决此问题..好的,您不能删除它,但可以通过删除缓存的子目录来清除缓存。删除缓存的子目录可以。但缓存不清除。注意:一旦关闭firefox,所有文件都将被删除,否则我无法删除它……你知道吗?你的意思是当firefox运行时,它将不允许删除子目录,但我尝试过,它甚至在firefox运行时工作正常。也许你的要求有所不同。
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 { }