C# IE注册和清除缓存
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; namespace LIZH { /// <summary> /// IE注册管理 /// </summary> public class IERegeditManager { public static void RegeditIE11() { try { //\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine .OpenSubKey(@"SOFTWARE"); if (key != null) key = key.OpenSubKey(@"Microsoft"); if (key != null) key = key.OpenSubKey(@"Internet Explorer"); if (key != null) key = key.OpenSubKey(@"Main"); if (key != null) key = key.OpenSubKey(@"FeatureControl"); if (key != null) { key = key.OpenSubKey(@"FEATURE_BROWSER_EMULATION", true); if (key != null) { var efs = key.GetValue("EFSProConsole.exe"); if (efs == null) { key.SetValue("EFSProConsole.exe", 11001, Microsoft.Win32.RegistryValueKind.DWord); //调试运行需要加上,否则不起作用 key.SetValue("EFSProConsole.vshost.exe", 11001, Microsoft.Win32.RegistryValueKind.DWord); } } } //\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION Microsoft.Win32.RegistryKey key64 = Microsoft.Win32.Registry.LocalMachine .OpenSubKey(@"SOFTWARE"); if (key64 != null) key64 = key64.OpenSubKey(@"WOW6432Node"); if (key64 != null) key64 = key64.OpenSubKey(@"Microsoft"); if (key64 != null) key64 = key64.OpenSubKey(@"Internet Explorer"); if (key64 != null) key64 = key64.OpenSubKey(@"Main"); if (key64 != null) key64 = key64.OpenSubKey(@"FeatureControl"); if (key64 != null) { key64 = key64.OpenSubKey(@"FEATURE_BROWSER_EMULATION", true); if (key64 != null) { var efs = key64.GetValue("EFSProConsole.exe"); if (efs == null) { key64.SetValue("EFSProConsole.exe", 11001, Microsoft.Win32.RegistryValueKind.DWord); //调试运行需要加上,否则不起作用 key64.SetValue("EFSProConsole.vshost.exe", 11001, Microsoft.Win32.RegistryValueKind.DWord); } } } } catch (Exception) { } } /// <summary> /// 清除IE缓存 /// </summary> public static void ClearIECache() { var specialFolders = new List<Environment.SpecialFolder>() { Environment.SpecialFolder.InternetCache, Environment.SpecialFolder.Cookies, Environment.SpecialFolder.History, }; foreach (var item in specialFolders) { try { var directory = new DirectoryInfo(Environment.GetFolderPath(item)); ClearFolder(directory); } catch (Exception ex) { } } } private static void ClearFolder(DirectoryInfo folder) { foreach (FileInfo file in folder.GetFiles()) { if (File.Exists(file.FullName)) { try { file.Delete(); } catch (Exception ex) { } } } foreach (DirectoryInfo directory in folder.GetDirectories()) { if (Directory.Exists(directory.FullName)) { var security = new System.Security.AccessControl.DirectorySecurity(); var windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent(); if (windowsIdentity != null) { var id = windowsIdentity.User; var rule = new System.Security.AccessControl.FileSystemAccessRule(id, System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow); security.AddAccessRule(rule); directory.SetAccessControl(security); } ClearFolder(directory); } } } /// <summary> /// 清除IE缓存 /// </summary> /// <param name="hwnd"></param> /// <param name="lpOperation"></param> /// <param name="lpFile"></param> /// <param name="lpParameters"></param> /// <param name="lpDirectory"></param> /// <param name="nShowCmd"></param> /// <returns></returns> [DllImport("shell32.dll")] public static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd); } public enum ShowCommands : int { SW_HIDE = 0, SW_SHOWNORMAL = 1, SW_NORMAL = 1, SW_SHOWMINIMIZED = 2, SW_SHOWMAXIMIZED = 3, SW_MAXIMIZE = 3, SW_SHOWNOACTIVATE = 4, SW_SHOW = 5, SW_MINIMIZE = 6, SW_SHOWMINNOACTIVE = 7, SW_SHOWNA = 8, SW_RESTORE = 9, SW_SHOWDEFAULT = 10, SW_FORCEMINIMIZE = 11, SW_MAX = 11 } }