本帖最后由 苏紫方璇 于 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');
} |