@@ -248,11 +248,15 @@ private RegistryKey CreateKeyInRegistryView(string keyName, RegistryKeyPermissio
248248 }
249249
250250 private void SetKeyInRegistryView ( string keyName , RegistryView registryView ) {
251- using ( RegistryKey registryKey = CreateKeyInRegistryView ( keyName , RegistryKeyPermissionCheck . Default , registryView ) ) {
252- if ( registryKey == null ) {
253- // key is invalid
254- throw new ArgumentException ( "The key \" " + keyName + "\" is invalid." ) ;
251+ try {
252+ using ( RegistryKey registryKey = CreateKeyInRegistryView ( keyName , RegistryKeyPermissionCheck . Default , registryView ) ) {
253+ if ( registryKey == null ) {
254+ // key is invalid
255+ throw new ArgumentException ( "The key \" " + keyName + "\" is invalid." ) ;
256+ }
255257 }
258+ } catch ( UnauthorizedAccessException ) {
259+ // key exists and we can't set it
256260 }
257261 }
258262
@@ -314,45 +318,45 @@ private object GetValueInRegistryView(string keyName, string valueName, out Regi
314318 }
315319
316320 private void SetValueInRegistryView ( string keyName , string valueName , object value , RegistryValueKind valueKind , RegistryView registryView ) {
317- using ( RegistryKey registryKey = CreateKeyInRegistryView ( keyName , RegistryKeyPermissionCheck . ReadWriteSubTree , registryView ) ) {
318- if ( registryKey == null ) {
319- // key is invalid
320- throw new ArgumentException ( "The key \" " + keyName + " \" is invalid." ) ;
321+ switch ( valueKind ) {
322+ case RegistryValueKind . Binary :
323+ if ( value is string binaryValue ) {
324+ value = Convert . FromBase64String ( binaryValue ) ;
321325 }
326+ break ;
327+ case RegistryValueKind . MultiString :
328+ if ( value is string multiStringValue ) {
329+ value = multiStringValue . Split ( '\0 ' ) ;
330+ }
331+ break ;
332+ }
322333
323- switch ( valueKind ) {
324- case RegistryValueKind . Binary :
325- if ( value is string binaryValue ) {
326- value = Convert . FromBase64String ( binaryValue ) ;
327- }
328- break ;
329- case RegistryValueKind . MultiString :
330- if ( value is string multiStringValue ) {
331- value = multiStringValue . Split ( '\0 ' ) ;
334+ try {
335+ using ( RegistryKey registryKey = CreateKeyInRegistryView ( keyName , RegistryKeyPermissionCheck . ReadWriteSubTree , registryView ) ) {
336+ if ( registryKey == null ) {
337+ // key is invalid
338+ throw new ArgumentException ( "The key \" " + keyName + "\" is invalid." ) ;
332339 }
333- break ;
334- }
335340
336- try {
337341 registryKey . SetValue ( valueName , value , valueKind ) ;
338- } catch {
339- try {
340- object _value = GetValueInRegistryView ( keyName , valueName , out RegistryValueKind ? _valueKind , registryView ) ;
341-
342- // if it's an exact string match, don't worry about it
343- if ( valueKind == _valueKind ) {
344- if ( value is string valueString
345- && _value is string _valueString ) {
346- if ( valueString . Equals ( _valueString , StringComparison . Ordinal ) ) {
347- return ;
348- }
342+ }
343+ } catch ( UnauthorizedAccessException ) {
344+ try {
345+ object _value = GetValueInRegistryView ( keyName , valueName , out RegistryValueKind ? _valueKind , registryView ) ;
346+
347+ // if it's an exact string match, don't worry about it
348+ if ( valueKind == _valueKind ) {
349+ if ( value is string valueString
350+ && _value is string _valueString ) {
351+ if ( valueString . Equals ( _valueString , StringComparison . Ordinal ) ) {
352+ return ;
349353 }
350354 }
351- } catch {
352- // fail silently
353355 }
354- throw ;
356+ } catch {
357+ // fail silently
355358 }
359+ throw ;
356360 }
357361 }
358362
@@ -363,7 +367,7 @@ private void DeleteValueInRegistryView(string keyName, string valueName, Registr
363367 return ;
364368 }
365369
366- registryKey . DeleteValue ( valueName ) ;
370+ registryKey . DeleteValue ( valueName , false ) ;
367371 }
368372 }
369373
@@ -373,7 +377,6 @@ private void DeleteValueInRegistryView(string keyName, string valueName, Registr
373377 // key does not exist
374378 return null ;
375379 }
376-
377380 return registryKey . GetValueKind ( valueName ) ;
378381 }
379382 }
@@ -1015,9 +1018,6 @@ public override void Activate(string templateName) {
10151018 // key doesn't exist and we can't set it
10161019 LogExceptionToLauncher ( ex ) ;
10171020 throw new TaskRequiresElevationException ( "Setting the key \" " + keyName + "\" requires elevation." ) ;
1018- } catch ( UnauthorizedAccessException ex ) {
1019- // key exists and we can't set it
1020- LogExceptionToLauncher ( ex ) ;
10211021 } catch ( InvalidOperationException ex ) {
10221022 // key marked for deletion
10231023 LogExceptionToLauncher ( ex ) ;
0 commit comments