四联光电智能照明论坛

标题: C# 清除Cookies [打印本页]

作者: 风火石    时间: 2016-11-8 13:15
标题: C# 清除Cookies
现在越来越多的网站使用了Cookie技术,它给我们带来方便的同时,也给我们带来了一点小麻烦。
比如Gmail的自动登录。
在网上没有找到合适的代码,于是就自己写了一个清除Cookie的函数(包括一个重载函数)。

CleanCookies方法

public bool CleanCookies(string URLStr, int ExType)
清除指定或全部Cookie数据

返回值
类型:BOOL
用于判断操作是否完成。
true表示成功 false表示失败

参  数
URLStr
类型:string
指定要删除的网址Cookies,赋空时,删除所有Cookies数据

ExType
类型:int
指定浏览器的类型
0为Internet Explorer 1为FireFox

重  载
public bool CleanCookies()
清除所有Cookie 默认IE浏览器

范  例
清除网站http://mail.google.com/的Cookie数据 使用IE浏览器
CleanCookies("mail.google.com",0);
清除网站http://mail.google.com/的Cookie数据 使用FireFox浏览器
CleanCookies("mail.google.com",1);
清除全部Cookie数据 使用IE浏览器
CleanCookies("",0);
使用重载函数删除所有Cookie数据
CleanCookies();

程  序
//清空126网站的Cookie数据
if (CleanCookies("www.126.com", 0))
         MessageBox.Show("清除成功");
else
         MessageBox.Show("清除失败");
源代码
  1. public bool CleanCookies(string URLStr, int ExType)
  2. {
  3.     //定义变量
  4.     string CookiesPath, FindDirctroy, OsTypeStr;
  5.     string UserProfile;
  6.     string XPCookiesPath, VistaCookiesPath;
  7.     long OsType;

  8.     //获取用户配置路径
  9.     UserProfile = Environment.GetEnvironmentVariable("USERPROFILE");
  10.     MessageBox.Show(UserProfile);
  11.     //获取操作系统类型
  12.     OsType = Environment.OSVersion.Version.Major;
  13.     //解析地址
  14.     if (URLStr == "")
  15.         FindDirctroy = "";
  16.     else
  17.     {
  18.         //用"."分割字符
  19.         char[] separator = { '.' };
  20.         string[] MyWords;
  21.         MyWords = URLStr.Split(separator);
  22.         //选取其中的关键字
  23.         try
  24.         {
  25.             FindDirctroy = MyWords[1];
  26.         }
  27.         catch
  28.         {
  29.             FindDirctroy = "";
  30.             //如果出错提示
  31.             MessageBox.Show("输入的网址格式不正确。");
  32.         }
  33.         //测试使用
  34.         MessageBox.Show(FindDirctroy);
  35.     }

  36.     //判断浏览器类型
  37.     if(ExType==0)
  38.     {
  39.         //IE浏览器
  40.         XPCookiesPath = @"\Cookies\";
  41.         VistaCookiesPath = @"\AppData\Roaming\Microsoft\Windows\Cookies\";
  42.     }
  43.     else if(ExType == 1)
  44.     {
  45.         //FireFox浏览器
  46.         XPCookiesPath = @"\Application Data\Mozilla\Firefox\Profiles\";
  47.         VistaCookiesPath = @"\AppData\Roaming\Mozilla\Firefox\Profiles\";
  48.         FindDirctroy = "cookies";
  49.     }
  50.     else
  51.     {
  52.      XPCookiesPath = "";
  53.      VistaCookiesPath = "";
  54.      return false;
  55.     }
  56.     //判断操作系统类型
  57.     if (OsType == 5)
  58.     {
  59.         //系统为XP
  60.         OsTypeStr = "Microsoft Windows XP";
  61.         CookiesPath = UserProfile + XPCookiesPath;
  62.         //测试使用
  63.         MessageBox.Show(CookiesPath);
  64.     }
  65.     else if (OsType == 6)
  66.     {
  67.         //系统为Vista
  68.         OsTypeStr = "Microsoft Windows Vista";
  69.         CookiesPath = UserProfile + VistaCookiesPath;
  70.         //测试使用
  71.         MessageBox.Show(CookiesPath);
  72.     }
  73.     else if (OsType == 7)
  74.     {
  75.         //系统为Win 7
  76.         OsTypeStr = "Microsoft Windows 7";
  77.         CookiesPath = UserProfile + VistaCookiesPath;
  78.         //测试使用
  79.         MessageBox.Show(CookiesPath);
  80.     }
  81.     else
  82.     {
  83.         //未识别之操作系统
  84.         OsTypeStr = "Other OS Version";
  85.         CookiesPath = "";
  86.         return false;
  87.     }
  88.     //删除文件
  89.     if (DeleteFileFunction(CookiesPath, FindDirctroy))
  90.         return true;
  91.     else
  92.         return false;
  93. }
  94. //重载函数
  95. public bool CleanCookies()
  96. {
  97.     if (CleanCookies("", 0))
  98.         return true;
  99.     else
  100.         return false;
  101. }
  102. private bool DeleteFileFunction(string filepath,string FindWords)
  103. {
  104.     string Dstr;
  105.     //下面这些字串例外
  106.     string ExceptStr = "index.dat";
  107.     //解析删除关键字
  108.     if (FindWords == "")
  109.         Dstr = "*.*";
  110.     else
  111.         Dstr = "*" + FindWords + "*";
  112.     //删除cookie文件夹
  113.     try
  114.     {
  115.         foreach (string dFileName in Directory.GetFiles(filepath,Dstr))
  116.         {
  117.             if (dFileName == filepath + ExceptStr)
  118.                 continue;
  119.             File.Delete(dFileName);
  120.         }
  121.     }
  122.     catch (Exception e)
  123.     {
  124.         MessageBox.Show("Cookies删除失败!\n" + e.ToString());
  125.         return false;
  126.     }
  127.     //深层遍历(解决Vista Low权限问题)
  128.     string[] LowPath = Directory.GetDirectories(filepath);
  129.     foreach (string ThePath in LowPath)
  130.     {
  131.         try
  132.         {
  133.             foreach (string dFileName in Directory.GetFiles(ThePath, Dstr))
  134.             {
  135.                 if (dFileName == filepath + ExceptStr)
  136.                     continue;
  137.                 File.Delete(dFileName);
  138.             }
  139.         }
  140.         catch (Exception e)
  141.         {
  142.             MessageBox.Show("遍历文件删除失败!\n" + e.ToString());
  143.             return false;
  144.         }
  145.     }
  146.     //测试使用
  147.     MessageBox.Show("删除完成!");
  148.     return true;
  149. }
复制代码





欢迎光临 四联光电智能照明论坛 (http://5xhome.com/) Powered by Discuz! X3.2