gentoo90 / winreg-rs

Rust bindings to MS Windows Registry API
MIT License
168 stars 36 forks source link

Add `delete_subkey_all_transacted` #35

Open maroider opened 4 years ago

maroider commented 4 years ago

It would be nice to be able to just run it on a key I know I "own" and have it be tied to a transaction.

gentoo90 commented 4 years ago

*_transacted methods just call *TransactedW functions from WinAPI. There is RegDeleteKeyTransactedW but there is no RegDeleteTreeTransactedW.

But you can open transacted key and then delete everything in it with delete_subkey_all:

use std::io;
use winreg::enums::*;
use winreg::transaction::Transaction;
use winreg::RegKey;

fn main() -> io::Result<()> {
    let t = Transaction::new()?;
    let hkcu = RegKey::predef(HKEY_CURRENT_USER);

    // ==========================================
    let key = hkcu.open_subkey_transacted_with_flags("PATH\\TO\\YOUR\\KEY", &t, KEY_ALL_ACCESS)?;
    key.delete_subkey_all("")?;
    // ==========================================

    println!("Delete everything in the key? [y/N]:");
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    input = input.trim_end().to_owned();
    if input == "y" || input == "Y" {
        t.commit()?;
        println!("Transaction committed.");
    } else {
        t.rollback()?;
        println!("Transaction wasn't committed, it will be rolled back.");
    }

    Ok(())
}