Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete_subkey_all_transacted #35

Open
maroider opened this issue Sep 26, 2020 · 1 comment
Open

Add delete_subkey_all_transacted #35

maroider opened this issue Sep 26, 2020 · 1 comment

Comments

@maroider
Copy link

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
Copy link
Owner

gentoo90 commented Oct 6, 2020

*_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(())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants