AI文摘 此内容由AI根据文章内容自动生成
AI Summary
支持作者:https://www.52pojie.cn/thread-2023697-1-1.html
众所周知微信低版本都会被强制更新,所以有了这个小工具 rust写的 很简单。
![图片[1]-禁止微信强制更新(开源)-大海资源库](https://attach.52pojie.cn/forum/202504/12/215228pvg87o7b8p7vi6tf.jpg)
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(())
}© 版权声明
NOTICE
1240555208@qq.com|Copyright © 2023~2026
01
DISCLAIMER
本站资源大多来自网络,如有侵犯权益请联系管理员,我们会第一时间审核删除。站内资源仅供学习测试,未经许可禁止商用,请在24小时内删除。
02
VIP PRIVILEGE
遇到付费内容?升级终身VIP即可全站免费畅享所有资源,可以联系我的微信进行开通。
大海聊天 QQ 3群:478065589
THE END









子比主题美化
精品源码资源 

暂无评论内容