前言
作为一个Rust新手,我在官网学习相关知识后动手实践Demo项目,期望通过互相交流学习共同进步,若有任何问题,还请大家不吝赐教。
一、Cargo.toml
[package]
name = "ytdxwinreg"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
winreg = "0.52"
二、main.rs
use std::io;
use winreg::enums::HKEY_CLASSES_ROOT;
use winreg::RegKey;
fn main() -> io::Result<()> {
let is_has_ytdx_key = RegKey::predef(HKEY_CLASSES_ROOT)
.enum_keys()
.map(|x| x.unwrap())
.filter(|x| x.starts_with("."))
.any(|s| s.contains(".ytdx"));
if is_has_ytdx_key {
println!("reg keys contains '.ytdx'");
} else {
let hkcr = RegKey::predef(HKEY_CLASSES_ROOT);
let (ytdx, _) = hkcr.create_subkey(&format!(".ytdx")).expect("failed");
ytdx.set_value("", &format!("ytdx_file")).expect("failed");
let (ytdx_file, _) = hkcr.create_subkey(&format!("ytdx_file")).expect("failed");
let (ytdx_file_defaulticon, _) = ytdx_file
.create_subkey(&format!("DefaultIcon"))
.expect("failed");
ytdx_file_defaulticon
.set_value(
"",
&format!(r#"D:\worker\wiring-diagram-editor-ui\src-tauri\icons\128x128.png"#),
)
.expect("failed");
let (ytdx_file_shell_open_command, _) = ytdx_file
.create_subkey(&format!("shell\\open\\command"))
.expect("failed");
ytdx_file_shell_open_command
.set_value(
"",
&format!(
r#"D:\worker\wiring-diagram-editor-ui\src-tauri\target\debug\云图.exe "%1""#
),
)
.expect("failed");
}
Ok(())
}