Winforms WebBrowser:如何删除Cookie [英] Winforms WebBrowser: how to delete cookies

查看:184
本文介绍了Winforms WebBrowser:如何删除Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除webbrowser控件中的cookie。此代码删除了cookies文件,但在重新启动程序之前,cookie仍然在内存中。

  private void deleteCookies(string domain )
{
string cookiesPath =
Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
string [] unWantedCookies =
Directory.GetFiles(cookiesPath,*+ domain +*);
foreach(unWantedCookies中的字符串cookie)
{
File.Delete(cookie);
}
}

我尝试过这个代码:

  webBrowser1 = new WebBrowser(); 

但它没有工作。我想这是在环境变量的某个地方,但在哪里?

解决方案

你真的不应该使用文件IO删除cookies,实际上不是文件,但是在index.dat文件中的数据,有一个虚拟文件,但它不是真实的,删除它不会从内存中删除cookie(这是你的问题) - 也不建议使用你要更好地研究DeleteUrlCacheEntry来删除cookies,并且通过使用FindFirst / NextUrlCacheEntry循环遍历所有缓存项,一旦你开始循环,寻找缓存项目以cookie:开始,然后可以进一步查看cookie名称,创建时间(修改,同步,上次访问,到期等),以决定要删除哪些Cookie。



互联网上有许多很好的预编写代码,可以让你在IE中循环和删除缓存项,所以使用任何一个,你需要做的一切在DeleteUrlCacheEntry调用之前添加一个过滤器,以决定是否要为该项调用DeleteUrlCacheEntry,当然,如果这样做,它不仅会从内部数据库中删除缓存条目,还会将其从活动中删除内存,你也不会得到文件使用错误,因为它会在删除之前将其从内存和使用中释放出来。



如果需要,我可以knwo更多信息或帮助。再次搜索您想要的特定语言的DeleteUrlCacheEntry代码,嘿,它甚至存在于VB6,所以我相信你会没有找到c#的副本:)。



让我知道你如何相处。


i'm trying to delete cookies in webbrowser control. This code deleted the cookies file, but before restarting the program, the cookies are still in memory.

private void deleteCookies(string domain)
    {
        string cookiesPath =
            Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
        string[] unWantedCookies = 
            Directory.GetFiles(cookiesPath, "*" + domain + "*");
        foreach (string cookie in unWantedCookies)
        {
            File.Delete(cookie);
        }
    }

I tried this code:

webBrowser1 = new WebBrowser();

But it didn't work. i guess it's somewhere in the environment variables, but where?

解决方案

You really should not use File IO to delete cookies, as cookies are not actually files, but data in an index.dat file, there is a virtual file but it isn't real, and deleting it will not remove the cookie from memory (this is your problem) - it is also not advised to use javascript hack to do this.

You are better of looking into DeleteUrlCacheEntry to delete cookies, and loop through all cache items using FindFirst/NextUrlCacheEntry, once you start looping, look for cache items begining with "cookie: ", and then you can look further into the cookie name, creation time (modified, synced, last accessed, expiration, etc) to decide which cookies you want to delete.

There are many good examples of pre-written code on the internet that will allow you to loop and delete cache items in IE, so use any one of them, and all you'll need to do is add a filter before the DeleteUrlCacheEntry call to decide if you want to call DeleteUrlCacheEntry for that item or not, of course, if you do, it will not only delete the cache entry from the internal database, but it will also remove it from active memory, and you also will not get the "file is use" error, as it will release it from memory and usage before deleting it.

Let me knwo if you need more info or help. Once again, search for the DeleteUrlCacheEntry code for the particular language you want it for, hey, it even exists for VB6 so i'm sure you'll have no trouble finding a copy for c# :).

let me know how you get along.

这篇关于Winforms WebBrowser:如何删除Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆