private bool CopyRegistryKey(IRegistryWrapper key, string path, string destination, bool recurse, bool streamResult, bool streamFirstOnly) { bool flag = true; if (recurse && this.ErrorIfDestinationIsSourceOrChildOfSource(path, destination)) { return false; } tracer.WriteLine("destination = {0}", new object[] { destination }); IRegistryWrapper regkeyForPath = this.GetRegkeyForPath(destination, true); string childName = this.GetChildName(path); string parentPath = destination; if (regkeyForPath == null) { parentPath = this.GetParentPath(destination, null); childName = this.GetChildName(destination); regkeyForPath = this.GetRegkeyForPathWriteIfError(parentPath, true); } if (regkeyForPath == null) { return false; } string str3 = this.MakePath(parentPath, childName); string copyKeyAction = RegistryProviderStrings.CopyKeyAction; string copyKeyResourceTemplate = RegistryProviderStrings.CopyKeyResourceTemplate; string target = string.Format(base.Host.CurrentCulture, copyKeyResourceTemplate, new object[] { path, destination }); if (base.ShouldProcess(target, copyKeyAction)) { IRegistryWrapper wrapper2 = null; try { wrapper2 = regkeyForPath.CreateSubKey(childName); } catch (NotSupportedException exception) { base.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.InvalidOperation, childName)); } if (wrapper2 != null) { string[] valueNames = key.GetValueNames(); for (int i = 0; i < valueNames.Length; i++) { if (base.Stopping) { regkeyForPath.Close(); wrapper2.Close(); return false; } wrapper2.SetValue(valueNames[i], key.GetValue(valueNames[i], null, RegistryValueOptions.DoNotExpandEnvironmentNames), key.GetValueKind(valueNames[i])); } if (streamResult) { this.WriteRegistryItemObject(wrapper2, str3); if (streamFirstOnly) { streamResult = false; } } } } regkeyForPath.Close(); if (recurse) { string[] subKeyNames = key.GetSubKeyNames(); for (int j = 0; j < subKeyNames.Length; j++) { if (base.Stopping) { return false; } string str7 = this.MakePath(path, subKeyNames[j]); string str8 = this.MakePath(str3, subKeyNames[j]); IRegistryWrapper wrapper3 = this.GetRegkeyForPath(str7, false); bool flag2 = this.CopyRegistryKey(wrapper3, str7, str8, recurse, streamResult, streamFirstOnly); wrapper3.Close(); if (!flag2) { flag = flag2; } } } return flag; }
private object ResetRegistryKeyValue(IRegistryWrapper key, string valueName) { RegistryValueKind valueKind = key.GetValueKind(valueName); object obj2 = null; switch (valueKind) { case RegistryValueKind.Unknown: case RegistryValueKind.Binary: obj2 = new byte[0]; break; case RegistryValueKind.String: case RegistryValueKind.ExpandString: obj2 = ""; break; case RegistryValueKind.DWord: obj2 = 0; break; case RegistryValueKind.MultiString: obj2 = new string[0]; break; case RegistryValueKind.QWord: obj2 = 0L; break; } try { key.SetValue(valueName, obj2, valueKind); } catch (IOException exception) { base.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.WriteError, valueName)); } catch (SecurityException exception2) { base.WriteError(new ErrorRecord(exception2, exception2.GetType().FullName, ErrorCategory.PermissionDenied, valueName)); } catch (UnauthorizedAccessException exception3) { base.WriteError(new ErrorRecord(exception3, exception3.GetType().FullName, ErrorCategory.PermissionDenied, valueName)); } return obj2; }
private void CopyProperty(IRegistryWrapper sourceKey, IRegistryWrapper destinationKey, string sourceProperty, string destinationProperty, bool writeOnSuccess) { string propertyName = this.GetPropertyName(sourceProperty); this.GetPropertyName(destinationProperty); object obj2 = sourceKey.GetValue(sourceProperty); RegistryValueKind valueKind = sourceKey.GetValueKind(sourceProperty); destinationKey.SetValue(destinationProperty, obj2, valueKind); if (writeOnSuccess) { this.WriteWrappedPropertyObject(obj2, propertyName, sourceKey.Name); } }
private static RegistryValueKind GetValueKindForProperty(IRegistryWrapper key, string valueName) { try { return key.GetValueKind(valueName); } catch (ArgumentException) { } catch (IOException) { } catch (SecurityException) { } catch (UnauthorizedAccessException) { } return RegistryValueKind.Unknown; }
/// <summary> /// Helper to get RegistryValueKind for a Property /// </summary> /// <param name="key">RegistryKey containing property</param> /// <param name="valueName">Property for which RegistryValueKind is requested</param> /// <returns>RegistryValueKind of the property. If the property does not exit,returns RegistryValueKind.Unknown</returns> private static RegistryValueKind GetValueKindForProperty(IRegistryWrapper key, string valueName) { try { return key.GetValueKind(valueName); } catch (System.ArgumentException) { //RegistryKey that contains the specified value does not exist } catch (System.IO.IOException) { } catch (System.Security.SecurityException) { } catch (System.UnauthorizedAccessException) { } return RegistryValueKind.Unknown; }
/// <summary> /// IT resets the a registry key value to its default /// </summary> /// <param name="key">Key whose value has to be reset</param> /// <param name="valueName">name of the value to reset</param> /// <returns>default value the key was set to</returns> private object ResetRegistryKeyValue(IRegistryWrapper key, string valueName) { RegistryValueKind valueKind = key.GetValueKind(valueName); object defaultValue = null; switch (valueKind) { // NOTICE: we assume that an unknown type is treated as // the same as a binary blob case RegistryValueKind.Binary: case RegistryValueKind.Unknown: { defaultValue = new byte[0]; } break; case RegistryValueKind.DWord: { defaultValue = (int)0; } break; case RegistryValueKind.ExpandString: case RegistryValueKind.String: { defaultValue = ""; } break; case RegistryValueKind.MultiString: { defaultValue = new string[0]; } break; case RegistryValueKind.QWord: { defaultValue = (long)0; } break; } try { key.SetValue(valueName, defaultValue, valueKind); } catch (System.IO.IOException ioException) { // An exception occurred while trying to set the value. Write // out the error. WriteError(new ErrorRecord(ioException, ioException.GetType().FullName, ErrorCategory.WriteError, valueName)); } catch (System.Security.SecurityException securityException) { // An exception occurred while trying to set the value. Write // out the error. WriteError(new ErrorRecord(securityException, securityException.GetType().FullName, ErrorCategory.PermissionDenied, valueName)); } catch (System.UnauthorizedAccessException unauthorizedAccessException) { // An exception occurred while trying to get the key. Write // out the error. WriteError(new ErrorRecord(unauthorizedAccessException, unauthorizedAccessException.GetType().FullName, ErrorCategory.PermissionDenied, valueName)); } return defaultValue; }
private void CopyProperty( IRegistryWrapper sourceKey, IRegistryWrapper destinationKey, string sourceProperty, string destinationProperty, bool writeOnSuccess) { string realSourceProperty = GetPropertyName(sourceProperty); string realDestinationProperty = GetPropertyName(destinationProperty); object sourceValue = sourceKey.GetValue(sourceProperty); RegistryValueKind sourceKind = sourceKey.GetValueKind(sourceProperty); destinationKey.SetValue(destinationProperty, sourceValue, sourceKind); if (writeOnSuccess) { WriteWrappedPropertyObject(sourceValue, realSourceProperty, sourceKey.Name); } } // CopyProperty
} // CopyItem private bool CopyRegistryKey( IRegistryWrapper key, string path, string destination, bool recurse, bool streamResult, bool streamFirstOnly) { bool result = true; // Make sure we are not trying to do a recursive copy of a key // to itself or a child of itself. if (recurse) { if (ErrorIfDestinationIsSourceOrChildOfSource(path, destination)) { return false; } } Dbg.Diagnostics.Assert( key != null, "The key should have been validated by the caller"); Dbg.Diagnostics.Assert( !String.IsNullOrEmpty(path), "The path should have been validated by the caller"); Dbg.Diagnostics.Assert( !String.IsNullOrEmpty(destination), "The destination should have been validated by the caller"); s_tracer.WriteLine("destination = {0}", destination); // Get the parent key of the destination // If the destination already exists and is a key, then it becomes // the container of the source. If the key doesn't already exist // the parent of the destination path becomes the container of source. IRegistryWrapper newParentKey = GetRegkeyForPath(destination, true); string destinationName = GetChildName(path); string destinationParent = destination; if (newParentKey == null) { destinationParent = GetParentPath(destination, null); destinationName = GetChildName(destination); newParentKey = GetRegkeyForPathWriteIfError(destinationParent, true); } if (newParentKey == null) { // The key was not found. // An error should have been written by GetRegkeyForPathWriteIfError return false; } string destinationPath = MakePath(destinationParent, destinationName); // Confirm the copy item with the user string action = RegistryProviderStrings.CopyKeyAction; string resourceTemplate = RegistryProviderStrings.CopyKeyResourceTemplate; string resource = String.Format( Host.CurrentCulture, resourceTemplate, path, destination); if (ShouldProcess(resource, action)) { // Create new key under the parent IRegistryWrapper newKey = null; try { newKey = newParentKey.CreateSubKey(destinationName); } catch (NotSupportedException e) { WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.InvalidOperation, destinationName)); } if (newKey != null) { // Now copy all the properties from the source to the destination string[] valueNames = key.GetValueNames(); for (int index = 0; index < valueNames.Length; ++index) { // Making sure to obey the StopProcessing. if (Stopping) { newParentKey.Close(); newKey.Close(); return false; } newKey.SetValue( valueNames[index], key.GetValue(valueNames[index], null, RegistryValueOptions.DoNotExpandEnvironmentNames), key.GetValueKind(valueNames[index])); } if (streamResult) { // Write out the key that was copied WriteRegistryItemObject(newKey, destinationPath); if (streamFirstOnly) { streamResult = false; } } } } newParentKey.Close(); if (recurse) { // Copy all the subkeys string[] subkeyNames = key.GetSubKeyNames(); for (int keyIndex = 0; keyIndex < subkeyNames.Length; ++keyIndex) { // Making sure to obey the StopProcessing. if (Stopping) { return false; } // Make the new path under the copy path. string subKeyPath = MakePath(path, subkeyNames[keyIndex]); string newSubKeyPath = MakePath(destinationPath, subkeyNames[keyIndex]); IRegistryWrapper childKey = GetRegkeyForPath(subKeyPath, false); bool subtreeResult = CopyRegistryKey(childKey, subKeyPath, newSubKeyPath, recurse, streamResult, streamFirstOnly); childKey.Close(); if (!subtreeResult) { result = subtreeResult; } } } return result; } // CopyRegistryKey