English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# code to clear cookies

Different browsers will save the cookie files in different places

  The following is the storage path of cookies for C# WebBrowser control

  C:\Users\{your account name}\AppData\Local\Microsoft\Windows\INetCookies

  Please refer to relevant materials for the format of the cookies file

The following is the code to clear cookies:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace DelCookies
{
class Program
{
static void Main(string[] args)
{
DelCookies("360.cn/");
Console.WriteLine("Cookies have been deleted.");
Console.Read();
}
static void DelCookies(string domain) //domain is the domain of the cookies, this method filters and clears cookies through the domain
{
//Get the file path in the directory
string[] cookies = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
foreach (string file in cookies)
{
try
{
StreamReader sr = new StreamReader(file);
string txt = sr.ReadToEnd();
sr.Close();
if (txt.IndexOf(domain) != -1) //Determine whether the cookies file is deleted
{
File.Delete(file);
}
}
catch (Exception ex)
{
}
} 
}
}
}

That's all for the code to clear cookies in C#. I hope it helps you. If you have any questions, please leave a message, and the editor will reply to you in time!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not undergo manual editing, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When reporting, please replace # with @ and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.)

You May Also Like