吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 7050|回复: 23
上一主题 下一主题
收起左侧

[C&C++ 原创] 参考[Windows] C盘清理工具做了个C++的,有点bug,希望大家一起完善。

  [复制链接]
跳转到指定楼层
楼主
pnews88 发表于 2025-4-17 16:32 回帖奖励
本帖最后由 苏紫方璇 于 2025-4-20 14:12 编辑

之前在https://www.52pojie.cn//thread-2022171-1-1.html,看了凤毛麟角大佬的C盘清理工具,大佬是用rust写的,我机器上没有rust,就改写了C++的,但是运行起来会出现多个命令窗体。有能力的,优化一下。
代码如下:
[C++] 纯文本查看 复制代码
#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
#include <fstream>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <limits>
#include <direct.h>
#include <shlobj.h>

//namespace fs = std::filesystem;

// 函数声明
bool is_admin();
bool verify_password();
std::string read_password_with_powershell();
void clean_temp_files();
void empty_recycle_bin();
void clean_browser_cache();
void clean_windows_update_cache();
void clean_windows_backup();
void clean_log_files();
void clean_defender_files();
void clean_iis_logs();
void delete_directory_contents(const std::string& dir);
void pause();
bool directory_exists(const std::string& dir);

int main() {
    std::cout << "Windows 系统垃圾清理工具" << std::endl;
    std::cout << "========================" << std::endl;
   
    // 检查管理员权限
    if (!is_admin()) {
        std::cout << "警告: 此程序需要管理员权限才能完全清理所有垃圾文件。" << std::endl;
        std::cout << "请右键点击程序,选择'以管理员身份运行'。" << std::endl;
        pause();
        return 0;
    }

    // 密码验证
    if (!verify_password()) {
        std::cout << "密码错误,程序退出。" << std::endl;
        return 0;
    }

    std::cout << "\n密码验证成功,开始执行清理操作..." << std::endl;
   
    // 可以选择先运行一次cleanmgr,设置所有清理选项
    std::cout << "正在配置系统清理选项..." << std::endl;
    //std::system("cleanmgr /sageset:65535");
   
    // 然后一次性运行所有清理任务
    std::cout << "正在执行系统清理..." << std::endl;
    std::system("cleanmgr /sagerun:65535");
   
    // 依次执行所有清理功能
    clean_temp_files();
    empty_recycle_bin();
    clean_browser_cache();
    clean_windows_update_cache();
    clean_windows_backup();
    clean_log_files();
    clean_defender_files();
    clean_iis_logs();
   
    std::cout << "\n所有清理操作已完成!" << std::endl;
    pause();
   
    return 0;
}

// 检查目录是否存在
bool directory_exists(const std::string& dir) {
    DWORD ftyp = GetFileAttributesA(dir.c_str());
    if (ftyp == INVALID_FILE_ATTRIBUTES)
        return false;  // 不存在或错误
   
    return (ftyp & FILE_ATTRIBUTE_DIRECTORY);
}
// 密码验证函数
bool verify_password() {
    const std::string CORRECT_PASSWORD = "52pojie"; // 设置正确的密码
    int attempts = 3; // 允许尝试的次数
   
    while (attempts > 0) {
        std::cout << "请输入密码 (还剩 " << attempts << " 次尝试): ";
        std::cout.flush();
        
        // 使用PowerShell读取密码并显示星号
        std::string password = read_password_with_powershell();
        
        if (password == CORRECT_PASSWORD) {
            return true;
        } else {
            std::cout << "\n密码错误!" << std::endl;
            attempts--;
        }
    }
   
    return false;
}

// 使用PowerShell读取密码并显示星号
std::string read_password_with_powershell() {
    // 创建一个临时PowerShell脚本
    std::string ps_script =
        "$password = Read-Host -AsSecureString\n"
        "$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)\n"
        "$plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)\n"
        "Write-Output $plainPassword";
   
    // 将脚本写入临时文件
    std::string temp_file = std::tmpnam(NULL);
    temp_file += ".ps1";
    std::ofstream script_file(temp_file);
    script_file << ps_script;
    script_file.close();
   
    // 执行PowerShell脚本
    std::string cmd = "powershell -ExecutionPolicy Bypass -File \"" + temp_file + "\" > temp_pwd.txt";
    int result = std::system(cmd.c_str());
   
    // 读取结果
    std::string password;
    if (result == 0) {
        std::ifstream pwd_file("temp_pwd.txt");
        std::getline(pwd_file, password);
        pwd_file.close();
        std::remove("temp_pwd.txt");
    } else {
        // 如果PowerShell命令失败,回退到标准输入
        std::cout << "\n无法使用安全输入模式,请直接输入密码:";
        std::getline(std::cin, password);
    }
   
    // 清理临时文件
    std::remove(temp_file.c_str());
   
    return password;
}

// 检查是否具有管理员权限
bool is_admin() {
    int result = std::system("net session >nul 2>&1");
    return (result == 0);
}

// 清理临时文件
void clean_temp_files() {
    std::cout << "\n正在清理临时文件..." << std::endl;
   
    // 使用系统内置的磁盘清理工具
    std::cout << "使用系统磁盘清理工具清理临时文件..." << std::endl;
    std::system("cleanmgr /d C: /sagerun:1");
   
    // 清理系统临时文件夹
    std::vector<std::string> temp_dirs;
    char* temp = std::getenv("TEMP");
    char* tmp = std::getenv("TMP");
   
    temp_dirs.push_back(temp ? temp : "C:\\Windows\\Temp");
    temp_dirs.push_back(tmp ? tmp : "C:\\Windows\\Temp");
    temp_dirs.push_back("C:\\Windows\\Temp");
   
    for (const auto& dir : temp_dirs) {
        std::cout << "清理目录: " << dir << std::endl;
        std::string cmd = "cmd /c del /f /s /q \"" + dir + "\\*\"";
        std::system(cmd.c_str());
    }
   
    // 清理用户临时文件夹
    char* userprofile = std::getenv("USERPROFILE");
    if (userprofile) {
        std::string user_temp = std::string(userprofile) + "\\AppData\\Local\\Temp";
        std::cout << "清理目录: " << user_temp << std::endl;
        std::string cmd = "cmd /c del /f /s /q \"" + user_temp + "\\*\"";
        std::system(cmd.c_str());
    }
   
    // 清理预取文件
    std::string prefetch_dir = "C:\\Windows\\Prefetch";
    std::cout << "清理预取文件: " << prefetch_dir << std::endl;
    std::string cmd = "cmd /c del /f /s /q \"" + prefetch_dir + "\\*\"";
    std::system(cmd.c_str());
   
    std::cout << "临时文件清理完成!" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

// 清空回收站
void empty_recycle_bin() {
    std::cout << "\n正在清空回收站..." << std::endl;
   
    // 使用系统内置的磁盘清理工具
    //std::cout << "使用系统磁盘清理工具清理回收站..." << std::endl;
    //std::system("cleanmgr /sagerun:2");
   
    // 使用PowerShell命令作为备选方案
    int status = std::system("powershell -Command \"Clear-RecycleBin -Force -ErrorAction SilentlyContinue\"");
   
    if (status == 0) {
        std::cout << "回收站已清空!" << std::endl;
    } else {
        std::cout << "清空回收站时出错,可能需要管理员权限。" << std::endl;
    }
   
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

// 清理浏览器缓存
void clean_browser_cache() {
    std::cout << "\n正在清理浏览器缓存..." << std::endl;
   
    char* userprofile = std::getenv("USERPROFILE");
    if (userprofile) {
        // Chrome 缓存
        std::string chrome_cache = std::string(userprofile) + "\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Cache";
        if (directory_exists(chrome_cache)) {
            std::cout << "清理 Chrome 缓存: " << chrome_cache << std::endl;
            std::string cmd = "cmd /c del /f /s /q \"" + chrome_cache + "\\*\"";
            std::system(cmd.c_str());
        }
        
        // Edge 缓存
        std::string edge_cache = std::string(userprofile) + "\\AppData\\Local\\Microsoft\\Edge\\User Data\\Default\\Cache";
        if (directory_exists(edge_cache)) {
            std::cout << "清理 Edge 缓存: " << edge_cache << std::endl;
            std::string cmd = "cmd /c del /f /s /q \"" + edge_cache + "\\*\"";
            std::system(cmd.c_str());
        }
        
        // Firefox 缓存
        std::string firefox_cache = std::string(userprofile) + "\\AppData\\Local\\Mozilla\\Firefox\\Profiles";
        if (directory_exists(firefox_cache)) {
            std::cout << "清理 Firefox 缓存: " << firefox_cache << std::endl;
            
            // 使用Windows命令查找所有Firefox配置文件夹
            std::string cmd = "cmd /c dir /b /ad \"" + firefox_cache + "\" > firefox_profiles.txt";
            std::system(cmd.c_str());
            
            std::ifstream profile_file("firefox_profiles.txt");
            std::string profile;
            while (std::getline(profile_file, profile)) {
                std::string cache_dir = firefox_cache + "\\" + profile + "\\cache2";
                if (directory_exists(cache_dir)) {
                    std::string del_cmd = "cmd /c del /f /s /q \"" + cache_dir + "\\*\"";
                    std::system(del_cmd.c_str());
                }
            }
            profile_file.close();
            std::remove("firefox_profiles.txt");
        }
    }
   
    std::cout << "浏览器缓存清理完成!" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

// 清理Windows更新缓存
void clean_windows_update_cache() {
    std::cout << "\n正在清理Windows更新缓存..." << std::endl;
   
    // 使用系统内置的磁盘清理工具
    //std::cout << "使用系统磁盘清理工具清理Windows更新文件..." << std::endl;
    //std::system("cleanmgr /sagerun:3");
   
    // 清理Windows更新下载文件
    std::cout << "清理Windows更新下载文件..." << std::endl;
    std::system("net stop wuauserv");
    std::system("cmd /c del /f /s /q %windir%\\SoftwareDistribution\\Download\\*");
    std::system("net start wuauserv");
   
    std::cout << "Windows更新缓存清理完成!" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

// 清理Windows备份文件
void clean_windows_backup() {
    std::cout << "\n正在清理Windows备份文件..." << std::endl;
   
    // 清理Windows.old文件夹(如果存在)
    std::string windows_old = "C:\\Windows.old";
    if (directory_exists(windows_old)) {
        std::cout << "发现 Windows.old 文件夹,尝试清理..." << std::endl;
        
        int status = std::system("rmdir /s /q C:\\Windows.old");
        
        if (status == 0) {
            std::cout << "Windows.old 文件夹已清理!" << std::endl;
        } else {
            std::cout << "清理 Windows.old 文件夹失败,可能需要使用磁盘清理工具。" << std::endl;
        }
    }
   
    // 清理系统还原点
    std::cout << "清理系统还原点..." << std::endl;
    std::system("vssadmin delete shadows /all /quiet");
   
    std::cout << "Windows备份文件清理完成!" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

// 清理系统日志文件
void clean_log_files() {
    std::cout << "\n正在清理系统日志文件..." << std::endl;
   
    // 使用系统内置的磁盘清理工具
    std::cout << "使用系统磁盘清理工具清理系统日志..." << std::endl;
    std::system("cleanmgr /sagerun:5");
   
    // 清理Windows错误报告
    std::cout << "清理Windows错误报告..." << std::endl;
    std::system("cleanmgr /sagerun:7");
   
    // 清理事件日志
    std::cout << "清理事件日志..." << std::endl;
    std::system("powershell -Command \"wevtutil el | Foreach-Object {wevtutil cl \\\"$_\\\"}\"");
   
    // 清理CBS日志
    std::string cbs_log = "C:\\Windows\\Logs\\CBS";
    std::cout << "清理 CBS 日志: " << cbs_log << std::endl;
    std::string cmd = "cmd /c del /f /s /q \"" + cbs_log + "\\*\"";
    std::system(cmd.c_str());
   
    std::cout << "系统日志文件清理完成!" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

// 清理Windows Defender文件
void clean_defender_files() {
    std::cout << "\n正在清理Windows Defender文件..." << std::endl;
   
    // 使用系统内置的磁盘清理工具
    std::system("cleanmgr /sagerun:4");
   
    std::cout << "Windows Defender文件清理完成!" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

// 清理IIS日志
void clean_iis_logs() {
    std::cout << "\n正在清理IIS日志..." << std::endl;
   
    // 使用系统内置的磁盘清理工具
    std::system("cleanmgr /sagerun:6");
   
    std::cout << "IIS日志清理完成!" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

// 删除目录中的所有内容
void delete_directory_contents(const std::string& dir) {
    if (!directory_exists(dir)) {
        return;
    }
   
    // 使用Windows命令删除目录内容
    std::string cmd = "cmd /c del /f /s /q \"" + dir + "\\*\"";
    std::system(cmd.c_str());
}
// 暂停程序执行,等待用户按键
void pause() {
    std::cout << "\n按回车键继续...";
    std::cout.flush();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

免费评分

参与人数 4吾爱币 +9 热心值 +4 收起 理由
xiaochenke + 1 热心回复!
yexu0304 + 1 + 1 用心讨论,共获提升!
cogi + 1 + 1 鼓励转贴优秀软件安全工具和文档!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

推荐
yixiaofan 发表于 2025-4-17 18:27
C盘我都是直接盲删,没办法,真搞不懂,前几天c盘满了,终于找到一个文件夹几十个G,删完发现微信聊天没了,在微信设置看了一下,更改一下聊天记录存储位置,还能把记录自动转到修改后的盘里,我看看我的微信,我那些记录啊。。。
推荐
wen4610078 发表于 2025-5-18 08:03
在外网下载了一个插件,然后怎么清除也清除不掉,不知道该怎么办了,也找不到那个文件在C盘哪里可以删除,求大佬写一个可以全部清除下载的文件,恢复系统原始状态
4#
vdavidyang 发表于 2025-4-17 20:02
其实直接做个批处理更方便点,环境都不用装
5#
E式丶男孩 发表于 2025-4-17 20:35
别人都是cpp转rust,你这rust转cpp
6#
苏紫方璇 发表于 2025-4-20 13:59
代码插入可以参考这个帖子
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn/thread-713042-1-1.html
(出处: 吾爱破解论坛)
7#
 楼主| pnews88 发表于 2025-4-26 00:55 |楼主
苏紫方璇 发表于 2025-4-20 13:59
代码插入可以参考这个帖子
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn/thr ...

学习了,真不会怎么插入代码
8#
 楼主| pnews88 发表于 2025-4-26 00:56 |楼主
E式丶男孩 发表于 2025-4-17 20:35
别人都是cpp转rust,你这rust转cpp

个性比较懒,不想学习新东西啊
9#
kulouxiaohai 发表于 2025-5-5 11:45
用C语言能写吗?
10#
naixubao 发表于 2025-6-18 08:36
有大佬弄个成品吗,非常感谢。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-2-3 11:34

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表