I am working on a webbrowser control on a Windows.form in c#. I have been trying to figure out a way to delete all cookies in my webbrowser. I have tried this without success.
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());}}}})())")
I also tried to delete all cookies like so.
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++;
}
}
This second way worked for me but only after I close the windows form and then open it. It seems to only delete all cookies after the windows form is closed. I need a way to delete all cookies from all places while the webbrowser is still open. Any help would be much appreciated.