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("清除失敗");
源代碼
public bool CleanCookies(string URLStr, int ExType)
{
//定義變量
string CookiesPath, FindDirctroy, OsTypeStr;
string UserProfile;
string XPCookiesPath, VistaCookiesPath;
long OsType;
//獲取用戶配置路徑
UserProfile = Environment.GetEnvironmentVariable("USERPROFILE");
MessageBox.Show(UserProfile);
//獲取操作系統類型
OsType = Environment.OSVersion.Version.Major;
//解析地址
if (URLStr == "")
FindDirctroy = "";
else
{
//用"."分割字符
char[] separator = { '.' };
string[] MyWords;
MyWords = URLStr.Split(separator);
//選取其中的關鍵字
try
{
FindDirctroy = MyWords[1];
}
catch
{
FindDirctroy = "";
//如果出錯提示
MessageBox.Show("輸入的網址格式不正確。");
}
//測試使用
MessageBox.Show(FindDirctroy);
}
//判斷瀏覽器類型
if(ExType==0)
{
//IE瀏覽器
XPCookiesPath = @"\Cookies\";
VistaCookiesPath = @"\AppData\Roaming\Microsoft\Windows\Cookies\";
}
else if(ExType == 1)
{
//FireFox瀏覽器
XPCookiesPath = @"\Application Data\Mozilla\Firefox\Profiles\";
VistaCookiesPath = @"\AppData\Roaming\Mozilla\Firefox\Profiles\";
FindDirctroy = "cookies";
}
else
{
XPCookiesPath = "";
VistaCookiesPath = "";
return false;
}
//判斷操作系統類型
if (OsType == 5)
{
//系統為XP
OsTypeStr = "Microsoft Windows XP";
CookiesPath = UserProfile + XPCookiesPath;
//測試使用
MessageBox.Show(CookiesPath);
}
else if (OsType == 6)
{
//系統為Vista
OsTypeStr = "Microsoft Windows Vista";
CookiesPath = UserProfile + VistaCookiesPath;
//測試使用
MessageBox.Show(CookiesPath);
}
else if (OsType == 7)
{
//系統為Win 7
OsTypeStr = "Microsoft Windows 7";
CookiesPath = UserProfile + VistaCookiesPath;
//測試使用
MessageBox.Show(CookiesPath);
}
else
{
//未識別之操作系統
OsTypeStr = "Other OS Version";
CookiesPath = "";
return false;
}
//刪除文件
if (DeleteFileFunction(CookiesPath, FindDirctroy))
return true;
else
return false;
}
//重載函數
public bool CleanCookies()
{
if (CleanCookies("", 0))
return true;
else
return false;
}
private bool DeleteFileFunction(string filepath,string FindWords)
{
string Dstr;
//下面這些字串例外
string ExceptStr = "index.dat";
//解析刪除關鍵字
if (FindWords == "")
Dstr = "*.*";
else
Dstr = "*" + FindWords + "*";
//刪除cookie文件夾
try
{
foreach (string dFileName in Directory.GetFiles(filepath,Dstr))
{
if (dFileName == filepath + ExceptStr)
continue;
File.Delete(dFileName);
}
}
catch (Exception e)
{
MessageBox.Show("Cookies刪除失敗!\n" + e.ToString());
return false;
}
//深層遍歷(解決Vista Low權限問題)
string[] LowPath = Directory.GetDirectories(filepath);
foreach (string ThePath in LowPath)
{
try
{
foreach (string dFileName in Directory.GetFiles(ThePath, Dstr))
{
if (dFileName == filepath + ExceptStr)
continue;
File.Delete(dFileName);
}
}
catch (Exception e)
{
MessageBox.Show("遍歷文件刪除失敗!\n" + e.ToString());
return false;
}
}
//測試使用
MessageBox.Show("刪除完成!");
return true;
}
星期一
c# 清除 cookie
訂閱:
張貼留言 (Atom)
1 則留言:
請問大大:
那GOOGLE瀏覽器是多少呢??(ExType值)
public bool CleanCookies(string URLStr, int ExType)
感謝大大 : )
張貼留言