|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
搬运来源:吾爱
发帖人:墨羽风
用rust编程写的 ai简单生成的 下面功能介绍和附带源代码
程序功能
1. 程序启动时会检查管理员权限
2. 要求用户输入密码(默认密码为 "52pojie"),输入时显示星号
3. 密码验证成功后,自动依次执行以下清理操作:
- 清理临时文件
- 清空回收站
- 清理浏览器缓存
- 清理 Windows 更新缓存
- 清理 Windows 备份文件
- 清理系统日志文件
- 清理 Windows Defender 文件
- 清理 IIS 日志
4. 每个清理操作完成后会暂停 1 秒,以便用户查看进度
5. 所有操作完成后,程序等待用户按回车键退出
工具下载链接:https://pan.baidu.com/s/15ej5aoAO6rH57_YujPVkXg?pwd=6666
下载链接2:https://pan.quark.cn/s/d9e047cbd7da
- use std::fs;
- use std::path::Path;
- use std::process::Command;
- use std::io::{self, Write};
- use std::env;
- use std::time::Duration;
- use std::thread;
-
- fn main() -> io::Result<()> {
- println!("Windows 系统垃圾清理工具");
- println!("========================");
-
- // 检查管理员权限
- if !is_admin() {
- println!("警告: 此程序需要管理员权限才能完全清理所有垃圾文件。");
- println!("请右键点击程序,选择'以管理员身份运行'。");
- pause()?;
- return Ok(());
- }
-
- // 密码验证
- if !verify_password()? {
- println!("密码错误,程序退出。");
- return Ok(());
- }
-
- println!("\n密码验证成功,开始执行清理操作...");
-
- // 依次执行所有清理功能
- 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()?;
-
- println!("\n所有清理操作已完成!");
- pause()?;
-
- Ok(())
- }
-
- // 密码验证函数
- fn verify_password() -> io::Result<bool> {
- const CORRECT_PASSWORD: &str = "52pojie"; // 设置正确的密码
- let mut attempts = 3; // 允许尝试的次数
-
- while attempts > 0 {
- print!("请输入密码 (还剩 {} 次尝试): ", attempts);
- io::stdout().flush()?;
-
- // 使用PowerShell读取密码并显示星号
- let password = read_password_with_powershell()?;
-
- if password == CORRECT_PASSWORD {
- return Ok(true);
- } else {
- println!("\n密码错误!");
- attempts -= 1;
- }
- }
-
- Ok(false)
- }
-
- // 使用PowerShell读取密码并显示星号
- fn read_password_with_powershell() -> io::Result<String> {
- // 创建一个临时PowerShell脚本
- let ps_script = r#"
- $password = Read-Host -AsSecureString
- $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
- $plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
- Write-Output $plainPassword
- "#;
-
- // 执行PowerShell脚本
- let output = Command::new("powershell")
- .args(&["-Command", ps_script])
- .output()?;
-
- if output.status.success() {
- // 转换输出为字符串并去除末尾的换行符
- let password = String::from_utf8_lossy(&output.stdout)
- .trim_end()
- .to_string();
-
- Ok(password)
- } else {
- // 如果PowerShell命令失败,回退到标准输入
- println!("\n无法使用安全输入模式,请直接输入密码:");
- let mut password = String::new();
- io::stdin().read_line(&mut password)?;
- Ok(password.trim().to_string())
- }
- }
-
- // 检查是否具有管理员权限
- fn is_admin() -> bool {
- if let Ok(output) = Command::new("net")
- .args(&["session"])
- .output() {
- output.status.success()
- } else {
- false
- }
- }
-
- // 清理临时文件
- fn clean_temp_files() -> io::Result<()> {
- println!("\n正在清理临时文件...");
-
- // 使用系统内置的磁盘清理工具
- println!("使用系统磁盘清理工具清理临时文件...");
- let _ = Command::new("cleanmgr")
- .args(&["/sagerun:1"])
- .status()?;
-
- // 清理系统临时文件夹
- let temp_dirs = vec![
- env::var("TEMP").unwrap_or_else(|_| String::from("C:\\Windows\\Temp")),
- env::var("TMP").unwrap_or_else(|_| String::from("C:\\Windows\\Temp")),
- String::from("C:\\Windows\\Temp"),
- ];
-
- for dir in temp_dirs {
- println!("清理目录: {}", dir);
- let _ = Command::new("cmd")
- .args(&["/c", &format!("del /f /s /q {}\\*", dir)])
- .status()?;
- }
-
- // 清理用户临时文件夹
- if let Ok(userprofile) = env::var("USERPROFILE") {
- let user_temp = format!("{}\\AppData\\Local\\Temp", userprofile);
- println!("清理目录: {}", user_temp);
- let _ = Command::new("cmd")
- .args(&["/c", &format!("del /f /s /q {}\\*", user_temp)])
- .status()?;
- }
-
- // 清理预取文件
- let prefetch_dir = "C:\\Windows\\Prefetch";
- println!("清理预取文件: {}", prefetch_dir);
- let _ = Command::new("cmd")
- .args(&["/c", &format!("del /f /s /q {}\\*", prefetch_dir)])
- .status()?;
-
- println!("临时文件清理完成!");
- thread::sleep(Duration::from_secs(1));
- Ok(())
- }
-
- // 清空回收站
- fn empty_recycle_bin() -> io::Result<()> {
- println!("\n正在清空回收站...");
-
- // 使用系统内置的磁盘清理工具
- println!("使用系统磁盘清理工具清理回收站...");
- let _ = Command::new("cleanmgr")
- .args(&["/sagerun:2"])
- .status()?;
-
- // 使用PowerShell命令作为备选方案
- let status = Command::new("powershell")
- .args(&["-Command", "Clear-RecycleBin -Force -ErrorAction SilentlyContinue"])
- .status()?;
-
- if status.success() {
- println!("回收站已清空!");
- } else {
- println!("清空回收站时出错,可能需要管理员权限。");
- }
-
- thread::sleep(Duration::from_secs(1));
- Ok(())
- }
-
- // 清理浏览器缓存
- fn clean_browser_cache() -> io::Result<()> {
- println!("\n正在清理浏览器缓存...");
-
- if let Ok(userprofile) = env::var("USERPROFILE") {
- // Chrome 缓存
- let chrome_cache = format!("{}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Cache", userprofile);
- if Path::new(&chrome_cache).exists() {
- println!("清理 Chrome 缓存: {}", chrome_cache);
- let _ = Command::new("cmd")
- .args(&["/c", &format!("del /f /s /q {}\\*", chrome_cache)])
- .status()?;
- }
-
- // Edge 缓存
- let edge_cache = format!("{}\\AppData\\Local\\Microsoft\\Edge\\User Data\\Default\\Cache", userprofile);
- if Path::new(&edge_cache).exists() {
- println!("清理 Edge 缓存: {}", edge_cache);
- let _ = Command::new("cmd")
- .args(&["/c", &format!("del /f /s /q {}\\*", edge_cache)])
- .status()?;
- }
-
- // Firefox 缓存
- let firefox_cache = format!("{}\\AppData\\Local\\Mozilla\\Firefox\\Profiles", userprofile);
- if Path::new(&firefox_cache).exists() {
- println!("清理 Firefox 缓存: {}", firefox_cache);
-
- if let Ok(entries) = fs::read_dir(&firefox_cache) {
- for entry in entries.flatten() {
- if let Ok(metadata) = entry.metadata() {
- if metadata.is_dir() {
- let cache_dir = format!("{}\\cache2", entry.path().to_string_lossy());
- if Path::new(&cache_dir).exists() {
- let _ = Command::new("cmd")
- .args(&["/c", &format!("del /f /s /q {}\\*", cache_dir)])
- .status()?;
- }
- }
- }
- }
- }
- }
- }
-
- println!("浏览器缓存清理完成!");
- thread::sleep(Duration::from_secs(1));
- Ok(())
- }
-
- // 清理Windows更新缓存
- fn clean_windows_update_cache() -> io::Result<()> {
- println!("\n正在清理Windows更新缓存...");
-
- // 使用系统内置的磁盘清理工具
- println!("使用系统磁盘清理工具清理Windows更新文件...");
- let _ = Command::new("cleanmgr")
- .args(&["/sagerun:3"])
- .status()?;
-
- // 清理Windows更新下载文件
- println!("清理Windows更新下载文件...");
- let _ = Command::new("net")
- .args(&["stop", "wuauserv"])
- .status()?;
-
- let _ = Command::new("cmd")
- .args(&["/c", "del /f /s /q %windir%\\SoftwareDistribution\\Download\\*"])
- .status()?;
-
- let _ = Command::new("net")
- .args(&["start", "wuauserv"])
- .status()?;
-
- // 清理Windows更新安装文件
- println!("清理Windows更新安装文件...");
- let _ = Command::new("dism")
- .args(&["/online", "/cleanup-image", "/startcomponentcleanup", "/resetbase"])
- .status()?;
-
- // 清理Windows更新备份文件
- println!("清理Windows更新备份文件...");
- let _ = Command::new("cleanmgr")
- .args(&["/sagerun:8"])
- .status()?;
-
- // 清理Windows更新日志
- println!("清理Windows更新日志...");
- let _ = Command::new("cleanmgr")
- .args(&["/sagerun:9"])
- .status()?;
-
- // 清理Windows更新临时文件
- println!("清理Windows更新临时文件...");
- let _ = Command::new("cleanmgr")
- .args(&["/sagerun:10"])
- .status()?;
-
- println!("Windows更新缓存清理完成!");
- thread::sleep(Duration::from_secs(1));
- Ok(())
- }
-
- // 清理Windows备份文件
- fn clean_windows_backup() -> io::Result<()> {
- println!("\n正在清理Windows备份文件...");
-
- // 清理Windows.old文件夹(如果存在)
- let windows_old = "C:\\Windows.old";
- if Path::new(windows_old).exists() {
- println!("发现 Windows.old 文件夹,尝试清理...");
-
- let status = Command::new("rmdir")
- .args(&["/s", "/q", windows_old])
- .status()?;
-
- if status.success() {
- println!("Windows.old 文件夹已清理!");
- } else {
- println!("清理 Windows.old 文件夹失败,可能需要使用磁盘清理工具。");
- }
- }
-
- // 清理系统还原点
- println!("清理系统还原点...");
- let _ = Command::new("vssadmin")
- .args(&["delete", "shadows", "/all", "/quiet"])
- .status()?;
-
- println!("Windows备份文件清理完成!");
- thread::sleep(Duration::from_secs(1));
- Ok(())
- }
-
- // 清理系统日志文件
- fn clean_log_files() -> io::Result<()> {
- println!("\n正在清理系统日志文件...");
-
- // 使用系统内置的磁盘清理工具
- println!("使用系统磁盘清理工具清理系统日志...");
- let _ = Command::new("cleanmgr")
- .args(&["/sagerun:5"])
- .status()?;
-
- // 清理Windows错误报告
- println!("清理Windows错误报告...");
- let _ = Command::new("cleanmgr")
- .args(&["/sagerun:7"])
- .status()?;
-
- // 清理事件日志
- println!("清理事件日志...");
- let _ = Command::new("powershell")
- .args(&["-Command", "wevtutil el | Foreach-Object {wevtutil cl "$_"}"])
- .status()?;
-
- // 清理CBS日志
- let cbs_log = "C:\\Windows\\Logs\\CBS";
- println!("清理 CBS 日志: {}", cbs_log);
- let _ = Command::new("cmd")
- .args(&["/c", &format!("del /f /s /q {}\\*", cbs_log)])
- .status()?;
-
- println!("系统日志文件清理完成!");
- thread::sleep(Duration::from_secs(1));
- Ok(())
- }
-
- // 清理Windows Defender文件
- fn clean_defender_files() -> io::Result<()> {
- println!("\n正在清理Windows Defender文件...");
-
- // 使用系统内置的磁盘清理工具
- let _ = Command::new("cleanmgr")
- .args(&["/sagerun:4"])
- .status()?;
-
- println!("Windows Defender文件清理完成!");
- thread::sleep(Duration::from_secs(1));
- Ok(())
- }
-
- // 清理IIS日志
- fn clean_iis_logs() -> io::Result<()> {
- println!("\n正在清理IIS日志...");
-
- // 使用系统内置的磁盘清理工具
- let _ = Command::new("cleanmgr")
- .args(&["/sagerun:6"])
- .status()?;
-
- println!("IIS日志清理完成!");
- thread::sleep(Duration::from_secs(1));
- Ok(())
- }
-
- // 删除目录中的所有内容
- #[allow(dead_code)]
- fn delete_directory_contents(dir: &str) -> io::Result<()> {
- if !Path::new(dir).exists() {
- return Ok(());
- }
-
- if let Ok(entries) = fs::read_dir(dir) {
- for entry in entries.flatten() {
- let path = entry.path();
-
- if let Ok(metadata) = entry.metadata() {
- if metadata.is_dir() {
- // 尝试递归删除子目录
- if let Err(e) = delete_directory_contents(&path.to_string_lossy()) {
- println!(" 无法清理 {}: {}", path.display(), e);
- }
-
- // 尝试删除空目录
- if let Err(e) = fs::remove_dir(&path) {
- println!(" 无法删除目录 {}: {}", path.display(), e);
- }
- } else {
- // 尝试删除文件
- if let Err(e) = fs::remove_file(&path) {
- println!(" 无法删除文件 {}: {}", path.display(), e);
- }
- }
- }
- }
- }
-
- Ok(())
- }
-
- // 暂停程序执行,等待用户按键
- fn pause() -> io::Result<()> {
- print!("\n按回车键继续...");
- io::stdout().flush()?;
- let mut input = String::new();
- io::stdin().read_line(&mut input)?;
- Ok(())
- }
复制代码 |
评分
-
查看全部评分
|