using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 常用工具集.Base;

namespace 常用工具集.ViewModel._04破解及系统相关
{
    public class 图标缓存清理ViewModel : ViewModelBase
    {
        public DelegateCommand ClearCmd { get; set; }

        public 图标缓存清理ViewModel()
        {
            ClearCmd = new DelegateCommand(ClearCmdFunc);
        }

        public void ClearCmdFunc(object obj)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("rem 关闭Windows外壳程序explorer");
            sb.AppendLine("taskkill /f /im explorer.exe");
            sb.AppendLine("rem 清理系统图标缓存数据库");
            sb.AppendLine("attrib -h -s -r \"%userprofile%\\AppData\\Local\\IconCache.db\"");
            sb.AppendLine("del /f \"%userprofile%\\AppData\\Local\\IconCache.db\"");
            sb.AppendLine("attrib /s /d -h -s -r \"%userprofile%\\AppData\\Local\\Microsoft\\Windows\\Explorer\\*\"");
            sb.AppendLine("del /f \"%userprofile%\\AppData\\Local\\Microsoft\\Windows\\Explorer\\thumbcache_32.db\"");
            sb.AppendLine("del /f \"%userprofile%\\AppData\\Local\\Microsoft\\Windows\\Explorer\\thumbcache_96.db\"");
            sb.AppendLine("del /f \"%userprofile%\\AppData\\Local\\Microsoft\\Windows\\Explorer\\thumbcache_102.db\"");
            sb.AppendLine("del /f \"%userprofile%\\AppData\\Local\\Microsoft\\Windows\\Explorer\\thumbcache_256.db\"");
            sb.AppendLine("del /f \"%userprofile%\\AppData\\Local\\Microsoft\\Windows\\Explorer\\thumbcache_1024.db\"");
            sb.AppendLine("del /f \"%userprofile%\\AppData\\Local\\Microsoft\\Windows\\Explorer\\thumbcache_idx.db\"");
            sb.AppendLine("del /f \"%userprofile%\\AppData\\Local\\Microsoft\\Windows\\Explorer\\thumbcache_sr.db\"");
            sb.AppendLine("rem 清理 系统托盘记忆的图标");
            sb.AppendLine("echo y|reg delete \"HKEY_CLASSES_ROOT\\Local Settings\\Software\\Microsoft\\Windows\\CurrentVersion\\TrayNotify\" /v IconStreams");
            sb.AppendLine("echo y|reg delete \"HKEY_CLASSES_ROOT\\Local Settings\\Software\\Microsoft\\Windows\\CurrentVersion\\TrayNotify\" /v PastIconsStream");
            sb.AppendLine("rem 重启Windows外壳程序explorer");
            sb.AppendLine("start explorer");
            string tempPath = Path.GetTempFileName();
            tempPath += ".bat";
            try
            {
                File.WriteAllText(tempPath, sb.ToString(), Encoding.GetEncoding("GB2312"));
            }
            catch
            {
                File.WriteAllText(tempPath, sb.ToString(), Encoding.UTF8);
            }
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName = tempPath,
                UseShellExecute = true, //不使用操作系统外壳程序启动
                CreateNoWindow = true
            };
            using (Process process = Process.Start(startInfo))
            {
                process.WaitForExit(); // 等待批处理执行完成
                File.Delete(tempPath);
            }
        }
    }
}
