How to delete the cookies create by a WebBrowser?

Hello,

I use a WebBrowser (C# .Net 2.0) and i don't know how i can delete the cookies created by the WebBrowser

Thanks for your help!


Answer this question

How to delete the cookies create by a WebBrowser?

  • CalinMac

    interesting.

    you can clear the cookie when the document has been loaded into your webbrowser control but no idea if this will remove it from the application/cookie cache:

    this.theWebBrowserControl.Document.Cookie = null;



  • Jean LeFrancais

    the WebBrowser uses IE as its core engine, so pretty much you have IE embedded in your application (all stripped out to the very core) so if you delete cookes in the webbrowser control, you are deleting cookies for the main IE application/webbrowser.

    if you still want to delete the cookies, then you need to use the File.Delete() method. Example:

     

    System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Cookies))

    be warned, you may have access denied (UnAuthorizedException) or IOException when deleting a file that cannot be deleted, so place a try catch block around it.

    You could also go through EACH cookie found in the cookie folder and then delete it and move on if there is an exception:

    string[] theCookies = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies))

     

    foreach(string currentFile in theCookies)

    {

       try

       {

          System.IO.File.Delete(currentFile);

       }

       catch(Exceptions...........) { ... }

    }



  • sooline

    Thank you.

    But why my cookie is stored for my application but not used by IE

    Example:
    I a use a WebBroser to go to Hotmail.com and set to store my mail and my password, if i close the application and run it again, hotmail don't ask me again my password.

    But when i open IE hotmail ask me my password! It's not the same cookie
    There is a different cookie by application


  • How to delete the cookies create by a WebBrowser?