I am using a WebBrowser control in my WPF application and I am looking to clear the IE cookie cache from code.
I have attempted to use the following code
string[] Cookies = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
foreach (string currentFile in Cookies)
{
try
{
System.IO.File.Delete(currentFile);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This deletes the files from the specified folder, but when I navigate back using the WebBrowser control, the cookies reappear as though they were never deleted.
This might be the right result, as I am assuming that the Environment.SpecialFolder.Cookies folder is in AppData which is a current snapshot of what the user is up to.
If I open up IE, and do a hard delete of cookies and cache I get the expected output in my app.
Is there a way to do this action in code?
Using below function one can delete cookie for the certain host name. If whole cache needs to be cleared then condition "if(sourceUrlName.Contains(hostEntry) && sourceUrlName.ToLower().Contains("cookie"))" should be removed.
The Microsoft KB Article will help you better and clear the entire Cache.
Link To Refer
However, You can try the following solution too.