gecko-dev/third_party/rust/winreg/examples/basic_usage.rs

51 строка
1.9 KiB
Rust

// Copyright 2015, Igor Shaula
// Licensed under the MIT License <LICENSE or
// http://opensource.org/licenses/MIT>. This file
// may not be copied, modified, or distributed
// except according to those terms.
extern crate winreg;
use std::path::Path;
use std::io;
use winreg::RegKey;
use winreg::enums::*;
fn main() {
println!("Reading some system info...");
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
let cur_ver = hklm.open_subkey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion").unwrap();
let pf: String = cur_ver.get_value("ProgramFilesDir").unwrap();
let dp: String = cur_ver.get_value("DevicePath").unwrap();
println!("ProgramFiles = {}\nDevicePath = {}", pf, dp);
let info = cur_ver.query_info().unwrap();
println!("info = {:?}", info);
println!("And now lets write something...");
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let path = Path::new("Software").join("WinregRsExample1");
let key = hkcu.create_subkey(&path).unwrap();
key.set_value("TestSZ", &"written by Rust").unwrap();
let sz_val: String = key.get_value("TestSZ").unwrap();
key.delete_value("TestSZ").unwrap();
println!("TestSZ = {}", sz_val);
key.set_value("TestDWORD", &1234567890u32).unwrap();
let dword_val: u32 = key.get_value("TestDWORD").unwrap();
println!("TestDWORD = {}", dword_val);
key.set_value("TestQWORD", &1234567891011121314u64).unwrap();
let qword_val: u64 = key.get_value("TestQWORD").unwrap();
println!("TestQWORD = {}", qword_val);
key.create_subkey("sub\\key").unwrap();
hkcu.delete_subkey_all(&path).unwrap();
println!("Trying to open nonexistent key...");
hkcu.open_subkey(&path)
.unwrap_or_else(|e| match e.kind() {
io::ErrorKind::NotFound => panic!("Key doesn't exist"),
io::ErrorKind::PermissionDenied => panic!("Access denied"),
_ => panic!("{:?}", e)
});
}