c# 底层封装cookie清除浏览器所有cookies
以下是使用C#底层封装清除浏览器所有cookies的代码:
using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
public class BrowserCookieClearer
{
public static void ClearAllCookies()
{
string[] browserPaths = GetBrowserPaths();
foreach (string browserPath in browserPaths)
{
ClearCookiesForBrowser(browserPath);
}
}
private static string[] GetBrowserPaths()
{
string[] browserPaths = new string[]
{
GetEdgeBrowserPath(),
GetChromeBrowserPath(),
GetFirefoxBrowserPath(),
GetIEBrowserPath(),
GetOperaBrowserPath(),
GetSafariBrowserPath()
};
return browserPaths;
}
private static void ClearCookiesForBrowser(string browserPath)
{
if (!string.IsNullOrEmpty(browserPath))
{
string[] cookiePaths = GetCookiePathsForBrowser(browserPath);
foreach (string cookiePath in cookiePaths)
{
if (File.Exists(cookiePath))
{
File.Delete(cookiePath);
}
}
}
}
private static string[] GetCookiePathsForBrowser(string browserPath)
{
string[] cookiePaths = null;
if (browserPath.Contains("MicrosoftEdge"))
{
cookiePaths = new string[]
{
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\Cookies"
};
}
else if (browserPath.Contains("Google"))
{
cookiePaths = new string[]
{
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Google\Chrome\User Data\Default\Cookies"
};
}
else if (browserPath.Contains("Firefox"))
{
cookiePaths = new string[]
{
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Mozilla\Firefox\Profiles\*\cookies.sqlite"
};
}
else if (browserPath.Contains("Internet Explorer"))
{
cookiePaths = new string[]
{
Environment.GetFolderPath(Environment.SpecialFolder.Cookies)
};
}
else if (browserPath.Contains("Opera"))
{
cookiePaths = new string[]
{
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Opera Software\Opera Stable\Cookies"
};
}
else if (browserPath.Contains("Safari"))
{
cookiePaths = new string[]
{
Environment.GetFolderPath(Environment.SpecialFolder.Cookies)
};
}
return cookiePaths;
}
private static string GetEdgeBrowserPath()
{
string path = null;
try
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\MicrosoftEdge\Capabilities\FileAssociations");
string id = key.GetValue(".htm").ToString();
key.Close();
key = Registry.ClassesRoot.OpenSubKey(id + @"\Application");
path = key.GetValue("ApplicationPath").ToString();
key.Close();
}
catch { }
return path;
}
private static string GetChromeBrowserPath()
{
string path = null;
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome");
path = key.GetValue("InstallLocation").ToString() + @"\Application\chrome.exe";
key.Close();
}
catch { }
return path;
}
private static string GetFirefoxBrowserPath()
{
string path = null;
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Mozilla\Mozilla Firefox");
path = key.GetValue("PathToExe").ToString();
key.Close();
}
catch { }
return path;
}
private static string GetIEBrowserPath()
{
string path = null;
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\IEXPLORE.EXE");
path = key.GetValue(null).ToString();
key.Close();
}
catch { }
return path;
}
private static string GetOperaBrowserPath()
{
string path = null;
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Opera Software");
string[] subKeyNames = key.GetSubKeyNames();
foreach (string subKeyName in subKeyNames)
{
key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Opera Software\" + subKeyName);
string pathValue = key.GetValue("Path").ToString();
string versionValue = key.GetValue("Version").ToString();
if (pathValue.EndsWith(@"\launcher.exe") && versionValue.StartsWith("1"))
{
path = pathValue.Replace(@"\launcher.exe", @"\opera.exe");
break;
}
}
key.Close();
}
catch { }
return path;
}
private static string GetSafariBrowserPath()
{
string path = null;
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Safari.exe");
path = key.GetValue(null).ToString();
key.Close();
}
catch { }
return path;
}
}
调用方式:
BrowserCookieClearer.ClearAllCookies();
``
原文地址: https://www.cveoy.top/t/topic/cEvB 著作权归作者所有。请勿转载和采集!