C#删除IE浏览器cookie

使用C#语言,通过读取配置文件,删除IE浏览器下的cookie文件,去除网页上推荐的与自己相关的网购信息。代码如下:

public class DeleteCookie
{

  public void ClearCookie()
  {
    string cookiePath = System.Configuration.ConfigurationSettings.AppSettings["cookiePath"];
    foreach (string singleFile in Directory.GetFileSystemEntries(cookiePath))
      {
        if (File.Exists(singleFile))
        {
          FileInfo fileInfo = new FileInfo(singleFile);
          if (fileInfo.Attributes.ToString().IndexOf("ReadOnly") != -1)

            {
            fileInfo.Attributes = FileAttributes.Normal;
            File.Delete(singleFile);//直接删除其中的文件

            }
        }

      }
  }
}

其中,在app.config文件中,添加如下节点:

<appSettings>
<add key="CookiePath" value="E:\\Users\\WCP\\AppData\\Local\\Microsoft\\Windows\\Temporary Internet Files"/>
</appSettings>

cookie文件的位置,可以在IE浏览器的“Internet选项”->“常规”->“清除历史记录”->“设置”->“查看文件”处,获得文件夹的全路径。

posted on 2014-03-10 23:01  猫腿饭  阅读(737)  评论(0)    收藏  举报