[Windows] 禁止微信强制更新(rust开源)

180it 2025-04-22 AM 33℃ 0条
use std::fs::{self, OpenOptions};
use std::io::{self, Read, Write};
use std::path::Path;
use std::process::Command;
 
fn main() {
    match run_program() {
        Ok(_) => {},
        Err(e) => {
            println!("程序出错: {}", e);
            println!("请确保以管理员权限运行此程序!");
            println!("按任意键退出...");
            let mut input = String::new();
            let _ = io::stdin().read_line(&mut input);
        }
    }
}
 
fn run_program() -> io::Result<()> {
    // 定义 hosts 文件路径 - 修正了路径中的 host 为 hosts
    let hosts_path = Path::new(r"C:\Windows\System32\drivers\etc\hosts");
     
    // 检查文件是否存在
    if !hosts_path.exists() {
        return Err(io::Error::new(io::ErrorKind::NotFound, "hosts 文件不存在"));
    }
     
    // 读取 hosts 文件内容
    let mut content = String::new();
    match fs::File::open(hosts_path) {
        Ok(mut file) => {
            file.read_to_string(&mut content)?;
        },
        Err(e) => {
            return Err(io::Error::new(io::ErrorKind::PermissionDenied, 
                format!("无法打开 hosts 文件: {}. 请确保以管理员权限运行", e)));
        }
    }
     
    // 检查是否已经包含禁止微信更新的配置
    let has_wechat_block = content.contains("127.0.0.1   dldir1.qq.com") && 
                           content.contains("127.0.0.1   dldir1v6.qq.com");
     
    if has_wechat_block {
        println!("您已经禁止微信更新");
    } else {
        println!("您还没有禁止微信更新");
        println!("输入1按回车以禁止微信更新");
         
        let mut input = String::new();
        io::stdin().read_line(&mut input)?;
         
        if input.trim() == "1" {
            // 添加禁止微信更新的配置
            match OpenOptions::new().write(true).append(true).open(hosts_path) {
                Ok(mut file) => {
                    // 确保文件末尾有换行符
                    if !content.ends_with('\n') {
                        writeln!(file)?;
                    }
                     
                    // 写入配置
                    writeln!(file, "127.0.0.1   dldir1.qq.com")?;
                    writeln!(file, "127.0.0.1   dldir1v6.qq.com")?;
                     
                    println!("修改成功,请重启电脑生效禁止功能。");
                    println!("是否马上重启电脑?按回车键重启,按其他键退出");
                     
                    let mut restart_input = String::new();
                    io::stdin().read_line(&mut restart_input)?;
                     
                    if restart_input.trim().is_empty() {
                        // 重启电脑
                        match Command::new("shutdown").args(&["/r", "/t", "0"]).spawn() {
                            Ok(_) => {},
                            Err(e) => println!("重启失败: {}", e)
                        }
                    }
                },
                Err(e) => {
                    return Err(io::Error::new(io::ErrorKind::PermissionDenied, 
                        format!("无法写入 hosts 文件: {}. 请确保以管理员权限运行", e)));
                }
            }
        }
    }
     
    // 程序结束前等待用户输入
    println!("按任意键退出...");
    let mut exit_input = String::new();
    io::stdin().read_line(&mut exit_input)?;
     
    Ok(())
}

来源:https://www.52pojie.cn/thread-2023697-1-1.html

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

[Windows] 禁止微信强制更新(rust开源)

上一篇 冰箱PTC芯片启动保护作用解析
下一篇 没有了