| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 | using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.InteropServices;namespace Optimizer{    internal static class CleanHelper    {        [DllImport("Shell32.dll")]        static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlag dwFlags);        // SYSTEM FOLDERS        internal static readonly string System32Folder = Environment.GetFolderPath(Environment.SpecialFolder.System);        internal static readonly string TempFolder = Path.GetTempPath();        internal static readonly string ProfileAppDataRoaming = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);        internal static readonly string ProgramData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);        internal static readonly string ProfileAppDataLocal = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);        //internal static readonly string ProfileAppDataLocalLow = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Low";        //internal static readonly string OSDrive = System32Folder.Substring(0, 3);        internal static readonly string OSDriveWindows = Environment.GetEnvironmentVariable("WINDIR", EnvironmentVariableTarget.Machine);        // INTERNET EXPLORER CACHE        static string[] ieCache =        {            Path.Combine(ProfileAppDataLocal, "Microsoft\\Windows\\INetCache\\IE"),            Path.Combine(ProfileAppDataLocal, "Microsoft\\Windows\\WebCache.old")        };        // CHROME FOLDERS        static string chromeFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "AppData\\Local\\Google\\Chrome\\User Data");        static string[] chromeUserDataCacheDirs = { "Default\\Cache", "Default\\Code Cache\\", "Default\\GPUCache\\", "ShaderCache", "Default\\Service Worker\\CacheStorage\\", "Default\\Service Worker\\ScriptCache\\", "GrShaderCache\\GPUCache\\", "Default\\File System\\", "Default\\JumpListIconsMostVisited\\", "Default\\JumpListIconsRecentClosed\\", "Default\\Service Worker\\Database" };        static string chromePasswordsDir = Path.Combine(ProfileAppDataLocal, "Google\\Chrome\\User Data\\Default\\Login Data");        static string[] chromeSessionDirs =        {            Path.Combine(ProfileAppDataLocal, "Google\\Chrome\\User Data\\Default\\Sessions"),            Path.Combine(ProfileAppDataLocal, "Google\\Chrome\\User Data\\Default\\Session Storage"),            Path.Combine(ProfileAppDataLocal, "Google\\Chrome\\User Data\\Default\\Extension State"),        };        static string[] chromeCookiesDirs =        {            Path.Combine(ProfileAppDataLocal, "Google\\Chrome\\User Data\\Default\\IndexedDB"),            Path.Combine(ProfileAppDataLocal, "Google\\Chrome\\User Data\\Default\\Cookies"),            Path.Combine(ProfileAppDataLocal, "Google\\Chrome\\User Data\\Default\\Cookies-journal")        };        static string[] chromeHistoryDirs =        {            Path.Combine(ProfileAppDataLocal, "Google\\Chrome\\User Data\\Default\\History"),            Path.Combine(ProfileAppDataLocal, "Google\\Chrome\\User Data\\Default\\History Provider Cache"),            Path.Combine(ProfileAppDataLocal, "Google\\Chrome\\User Data\\Default\\History-journal")        };        // FIREFOX FOLDERS        static string firefoxRoaming = Path.Combine(ProfileAppDataRoaming, "Mozilla\\Firefox\\Profiles");        static string firefoxLocal = Path.Combine(ProfileAppDataLocal, "Mozilla\\Firefox\\Profiles");        // EDGE FOLDERS        static string edgeHistory = Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\History");        static string[] edgeCookies =        {            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\Cookies"),            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\IndexedDB")        };        static string[] edgeSession =        {            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\Sessions"),            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\Session Storage"),            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\Extension State")        };        static string[] edgeCache =        {            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\Cache"),            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\Code Cache"),            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\GPUCache"),            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\ShaderCache"),            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\Service Worker\\CacheStorage"),            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\Service Worker\\ScriptCache"),            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\GrShaderCache\\GPUCache"),            Path.Combine(ProfileAppDataLocal, "Microsoft\\Edge\\User Data\\Default\\Service Worker\\Database"),        };        internal static List<string> PreviewCleanList = new List<string>();        internal static void PreviewFolder(string path)        {            try            {                if (File.Exists(path))                {                    PreviewCleanList.Add(path);                    return;                }                DirectoryInfo di = new DirectoryInfo(path);                foreach (FileInfo file in di.GetFiles("*", SearchOption.AllDirectories))                {                    try                    {                        PreviewCleanList.Add(file.FullName);                    }                    catch { }                }                foreach (DirectoryInfo dir in di.GetDirectories("*", SearchOption.AllDirectories))                {                    try                    {                        PreviewCleanList.Add(dir.FullName);                    }                    catch { }                }            }            catch { }        }        internal static void Clean()        {            foreach (string x in PreviewCleanList)            {                try                {                    if (Directory.Exists(x)) Directory.Delete(x);                    if (File.Exists(x)) File.Delete(x);                }                catch { }            }        }        internal static void EmptyRecycleBin()        {            SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlag.SHERB_NOSOUND | RecycleFlag.SHERB_NOCONFIRMATION);        }        internal static void PreviewTemp()        {            PreviewFolder(TempFolder);        }        internal static void PreviewMinidumps()        {            PreviewFolder(OSDriveWindows + "\\Minidump");        }        internal static void PreviewErrorReports()        {            PreviewFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportArchive");            PreviewFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportQueue");            PreviewFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\Temp");            PreviewFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ERC");            PreviewFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportArchive");            PreviewFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportQueue");            PreviewFolder(ProgramData + "\\Microsoft\\Windows\\WER\\Temp");            PreviewFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ERC");        }        internal static ByteSize CheckFootprint()        {            ByteSize totalSize = new ByteSize(0);            List<DirectoryInfo> dirs = new List<DirectoryInfo>();            try            {                foreach (string x in ieCache) dirs.Add(new DirectoryInfo(x));                dirs.Add(new DirectoryInfo(chromeFolder));                dirs.Add(new DirectoryInfo(@"C:\Users\deadmoon\AppData\Local\Microsoft\Edge"));                dirs.Add(new DirectoryInfo(firefoxLocal));                dirs.Add(new DirectoryInfo(firefoxRoaming));                dirs.Add(new DirectoryInfo(TempFolder));                foreach (DirectoryInfo di in dirs)                {                    try                    {                        if (!Directory.Exists(di.FullName)) { continue; }                        totalSize += totalSize.AddBytes(di.EnumerateFiles("*", SearchOption.AllDirectories).Sum(file => file.Length));                    }                    catch { continue; }                }            }            catch (Exception ex)            {                ErrorLogger.LogError("CleanHelper.CleanLogs", ex.Message, ex.StackTrace);                return totalSize;            }            return totalSize;        }        internal static void PreviewEdgeClean(bool cache, bool cookies, bool seachHistory, bool session)        {            if (cache)            {                foreach (string x in edgeCache)                {                    PreviewFolder(x);                }            }            if (cookies)            {                foreach (string x in edgeCookies)                {                    PreviewFolder(x);                }            }            if (seachHistory)            {                PreviewFolder(edgeHistory);            }            if (session)            {                foreach (string x in edgeSession)                {                    PreviewFolder(x);                }            }        }        internal static void PreviewInternetExplorerCache()        {            foreach (string x in ieCache)            {                PreviewFolder(x);            }        }        internal static void PreviewFireFoxClean(bool cache, bool cookies, bool searchHistory)        {            foreach (string x in Directory.EnumerateDirectories(firefoxRoaming))            {                if (x.ToLowerInvariant().Contains("release"))                {                    if (cookies)                    {                        PreviewFolder(Path.Combine(x, "cookies.sqlite"));                    }                    if (searchHistory)                    {                        PreviewFolder(Path.Combine(x, "places.sqlite"));                    }                    if (cache)                    {                        PreviewFolder(Path.Combine(x, "shader-cache"));                    }                }            }            if (cache)            {                foreach (string x in Directory.EnumerateFiles(firefoxLocal))                {                    if (x.ToLowerInvariant().Contains("release"))                    {                        PreviewFolder(Path.Combine(x, "cache2"));                    }                }            }        }        internal static void PreviewChromeClean(bool cache, bool cookies, bool searchHistory, bool session, bool passwords)        {            if (cache)            {                foreach (string x in chromeUserDataCacheDirs)                {                    PreviewFolder(Path.Combine(chromeFolder, x));                }            }            if (session)            {                foreach (string x in chromeSessionDirs)                {                    PreviewFolder(x);                }            }            if (cookies)            {                foreach (string x in chromeCookiesDirs)                {                    PreviewFolder(x);                }            }            if (searchHistory)            {                foreach (string x in chromeHistoryDirs)                {                    PreviewFolder(x);                }            }            if (passwords)            {                PreviewFolder(chromePasswordsDir);            }        }    }}
 |