Open one of predefined keys:
HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
HKEY_PERFORMANCE_DATA
HKEY_PERFORMANCE_TEXT
HKEY_PERFORMANCE_NLSTEXT
HKEY_CURRENT_CONFIG
HKEY_DYN_DATA
HKEY_CURRENT_USER_LOCAL_SETTINGS
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
Return inner winapi HKEY of a key:
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
let soft = hklm.open_subkey("SOFTWARE").unwrap();
let handle = soft.raw_handle();
Open subkey with KEY_READ
permissions.
Will open another handle to itself if path
is an empty string.
To open with different permissions use open_subkey_with_flags
.
You can also use create_subkey
to open with KEY_ALL_ACCESS
permissions.
let soft = RegKey::predef(HKEY_CURRENT_USER)
.open_subkey("Software").unwrap();
Open subkey with desired permissions.
Will open another handle to itself if path
is an empty string.
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
hklm.open_subkey_with_flags("SOFTWARE\\Microsoft", KEY_READ).unwrap();
Create subkey (and all missing parent keys)
and open it with KEY_ALL_ACCESS
permissions.
Will just open key if it already exists.
Will open another handle to itself if path
is an empty string.
To create with different permissions use create_subkey_with_flags
.
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let settings = hkcu.create_subkey("Software\\MyProduct\\Settings").unwrap();
Copy all the values and subkeys from path
to dest
key.
WIll copy the content of self
if path
is an empty string.
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let src = hkcu.open_subkey_with_flags("Software\\MyProduct", KEY_READ).unwrap();
let dst = hkcu.create_subkey("Software\\MyProduct\\Section2").unwrap();
src.copy_tree("Section1", &dst).unwrap();
Return an iterator over subkeys names.
println!("File extensions, registered in this system:");
for i in RegKey::predef(HKEY_CLASSES_ROOT)
.enum_keys().map(|x| x.unwrap())
.filter(|x| x.starts_with("."))
{
println!("{}", i);
}
Return an iterator over values.
let system = RegKey::predef(HKEY_LOCAL_MACHINE)
.open_subkey_with_flags("HARDWARE\\DESCRIPTION\\System", KEY_READ)
.unwrap();
for (name, value) in system.enum_values().map(|x| x.unwrap()) {
println!("{} = {:?}", name, value);
}
Delete key.Key names are not case sensitive.
Cannot delete if it has subkeys.
Use delete_subkey_all
for that.
RegKey::predef(HKEY_CURRENT_USER)
.delete_subkey(r"Software\MyProduct\History").unwrap();
Recursively delete subkey with all its subkeys and values.
If path
is an empty string, the subkeys and values of this key are deleted.
RegKey::predef(HKEY_CURRENT_USER)
.delete_subkey_all("Software\\MyProduct").unwrap();
Get a value from registry and seamlessly convert it to the specified rust type
with FromRegValue
implemented (currently String
, u32
and u64
).
Will get the Default
value if name
is an empty string.
let settings = hkcu.open_subkey("Software\\MyProduct\\Settings").unwrap();
let server: String = settings.get_value("server").unwrap();
let port: u32 = settings.get_value("port").unwrap();
Get raw bytes from registry value.
Will get the Default
value if name
is an empty string.
let settings = hkcu.open_subkey("Software\\MyProduct\\Settings").unwrap();
let data = settings.get_raw_value("data").unwrap();
println!("Bytes: {:?}", data.bytes);
Seamlessly convert a value from a rust type and write it to the registry value
with ToRegValue
trait implemented (currently String
, &str
, u32
and u64
).
Will set the Default
value if name
is an empty string.
let settings = hkcu.create_subkey("Software\\MyProduct\\Settings").unwrap();
settings.set_value("server", &"www.example.com").unwrap();
settings.set_value("port", &8080u32).unwrap();
Write raw bytes from RegValue
struct to a registry value.
Will set the Default
value if name
is an empty string.
use winreg::{RegKey, RegValue};
use winreg::enums::*;
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let settings = hkcu.open_subkey("Software\\MyProduct\\Settings").unwrap();
let bytes: Vec<u8> = vec![1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
let data = RegValue{ vtype: REG_BINARY, bytes: bytes};
settings.set_raw_value("data", &data).unwrap();
println!("Bytes: {:?}", data.bytes)
Delete specified value from registry.
Will delete the Default
value if name
is an empty string.
let settings = hkcu.open_subkey("Software\\MyProduct\\Settings").unwrap();
settings.delete_value("data").unwrap();