如何删除webbrowser windows.form中的所有cookie

时间:2016-05-25 15:57:52

标签: c# cookies webbrowser-control

我正在使用c#中的Windows.form上的webbrowser控件。我一直试图找到一种方法来删除我的webbrowser中的所有cookie。我试过这个没有成功。

webBrowser.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())")

我也试图删除所有的cookie。

string[] Cookies = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
                int notDeleted = 0;
                foreach (string CookieFile in Cookies)
                {
                    try
                    {
                        System.IO.File.Delete(CookieFile);

                    }
                    catch (Exception ex)
                    {
                        notDeleted++;
                    }

                }

第二种方式对我有用,但只有在我关闭窗体然后打开它之后。它似乎只在Windows窗体关闭后删除所有cookie。我需要一种方法在webbrowser仍处于打开状态时删除所有地方的所有cookie。任何帮助将不胜感激。

0 个答案:

没有答案