rust - 进程的命令恐慌无法访问已被使用的文件
问题描述
我试图在我的 Rust 代码中通过Comand::new
. CLI 文件正在从二进制文件中提取到 exe 文件,然后使用Command::new
. 但它给出了'ERROR: Os { code: 32, kind: Other, message: "The process cannot access the file because it is being used by another process." }'
错误。
let taskmgr_pid = get_pid_by_name("Taskmgr.exe");
let process_hide = asset::Asset::get("cli.exe").unwrap();
let file_path = "C:\\filepathhere\\cli.exe";
let mut file = File::create(file_path.to_string()).expect("Couldn't create file");
file.write_all(&process_hide);
let res = Command::new(file_path)
.arg(taskmgr_pid.to_string())
.output()
.expect("ERROR");
println!("PID: {}", taskmgr_pid);
println!("{:?}", res);
解决方案
file
这是因为您在执行命令之前没有关闭。解决问题的最简单方法是简单地drop(file);
在Command::new()
.
let mut file = File::create(file_path).expect("unable to create file");
file.write_all(&process_hide).expect("unable to write");
drop(file);
let res = Command::new(file_path)
.arg(taskmgr_pid.to_string())
.output()
.expect("ERROR");
推荐阅读
- google-maps - 我们可以在 GoogleMap 中初始化CameraPosition - Flutter
- azure - 为应用程序日志配置 Azure 监视器
- php - 如何对日历脚本运行多个查询以使一天不可用
- reactjs - vscode中使用antd框架的按钮提示
- rx-java2 - 使用`onBackpressureLatest`在阻塞Flowable中删除中间消息
- r - 情节不显示
- php - 如何替换之间的链接并保持href的链接不变?
- angular - 信号 R 连接被生产中的服务器终止
- java - 如何修复“StringIndexOutOfBoundsException”错误?
- javascript - 我对 new Promise((resolve, reject) => {[loop]}) 阻塞而 Promise.resolve().then([loop]) 不感到困惑