@@ -259,13 +259,22 @@ impl RegistryHelper {
259259 ///
260260 /// * `RegistryError` - The error that occurred.
261261 pub fn remove ( & self ) -> Result < Option < Registry > , RegistryError > {
262- let ( reg_key, _subkey) = match self . open ( Security :: AllAccess ) {
262+ // For deleting a value, we need SetValue permission (KEY_SET_VALUE).
263+ // Try to open with the minimal required permission.
264+ // If that fails due to permission, try with AllAccess as a fallback.
265+ let ( reg_key, _subkey) = match self . open ( Security :: SetValue ) {
263266 Ok ( reg_key) => reg_key,
264267 // handle NotFound error
265268 Err ( RegistryError :: RegistryKeyNotFound ( _) ) => {
266269 eprintln ! ( "{}" , t!( "registry_helper.removeErrorKeyNotExist" ) ) ;
267270 return Ok ( None ) ;
268271 } ,
272+ Err ( RegistryError :: RegistryKey ( key:: Error :: PermissionDenied ( _, _) ) ) => {
273+ match self . open ( Security :: AllAccess ) {
274+ Ok ( reg_key) => reg_key,
275+ Err ( e) => return self . handle_error_or_what_if ( e) ,
276+ }
277+ } ,
269278 Err ( e) => return self . handle_error_or_what_if ( e) ,
270279 } ;
271280
@@ -289,10 +298,11 @@ impl RegistryHelper {
289298 Err ( e) => return self . handle_error_or_what_if ( RegistryError :: RegistryValue ( e) ) ,
290299 }
291300 } else {
292- // to delete the key, we need to open the parent key first
301+ // to delete the key, we need to open the parent key with CreateSubKey permission
302+ // which includes the ability to delete subkeys
293303 let parent_path = get_parent_key_path ( & self . config . key_path ) ;
294304 let ( hive, parent_subkey) = get_hive_from_path ( parent_path) ?;
295- let parent_reg_key = match hive. open ( parent_subkey, Security :: AllAccess ) {
305+ let parent_reg_key = match hive. open ( parent_subkey, Security :: CreateSubKey ) {
296306 Ok ( k) => k,
297307 Err ( e) => return self . handle_error_or_what_if ( RegistryError :: RegistryKey ( e) ) ,
298308 } ;
0 commit comments