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 void SetRegistryValue(IRegistryWrapper key, string propertyName, object value, RegistryValueKind kind, string path, bool writeResult) { string valueName = this.GetPropertyName(propertyName); RegistryValueKind unknown = RegistryValueKind.Unknown; if (kind == RegistryValueKind.Unknown) { unknown = GetValueKindForProperty(key, valueName); } if (unknown != RegistryValueKind.Unknown) { try { value = ConvertValueToKind(value, unknown); kind = unknown; } catch (InvalidCastException) { unknown = RegistryValueKind.Unknown; } } if (unknown == RegistryValueKind.Unknown) { if (kind == RegistryValueKind.Unknown) { if (value != null) { kind = GetValueKindFromObject(value); } else { kind = RegistryValueKind.String; } } value = ConvertValueToKind(value, kind); } key.SetValue(valueName, value, kind); if (writeResult) { object obj2 = key.GetValue(valueName); this.WriteWrappedPropertyObject(obj2, propertyName, path); } }
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 void MoveProperty(IRegistryWrapper sourceKey, IRegistryWrapper destinationKey, string sourceProperty, string destinationProperty) { string propertyName = this.GetPropertyName(sourceProperty); string b = this.GetPropertyName(destinationProperty); try { bool flag = true; if (string.Equals(sourceKey.Name, destinationKey.Name, StringComparison.OrdinalIgnoreCase) && string.Equals(propertyName, b, StringComparison.OrdinalIgnoreCase)) { flag = false; } this.CopyProperty(sourceKey, destinationKey, propertyName, b, false); if (flag) { sourceKey.DeleteValue(propertyName); } object obj2 = destinationKey.GetValue(b); this.WriteWrappedPropertyObject(obj2, destinationProperty, destinationKey.Name); } catch (IOException exception) { base.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.WriteError, sourceKey.Name)); } catch (SecurityException exception2) { base.WriteError(new ErrorRecord(exception2, exception2.GetType().FullName, ErrorCategory.PermissionDenied, sourceKey.Name)); } catch (UnauthorizedAccessException exception3) { base.WriteError(new ErrorRecord(exception3, exception3.GetType().FullName, ErrorCategory.PermissionDenied, sourceKey.Name)); } }
private static object ReadExistingKeyValue(IRegistryWrapper key, string valueName) { try { return key.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames); } catch (IOException) { } catch (SecurityException) { } catch (UnauthorizedAccessException) { } return null; }
/// <summary> /// helper to read back an existing registry key value /// </summary> /// <param name="key">key to read the value from</param> /// <param name="valueName">name of the value to read</param> /// <returns>value of the key, null if it could not retrieve /// it because known exceptions were thrown, else an exception is percolated up /// </returns> private static object ReadExistingKeyValue(IRegistryWrapper key, string valueName) { try { // Since SetValue can munge the data to a specified // type (RegistryValueKind), retrieve the value again // to output it in the correct form to the user. return key.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames); } catch (System.IO.IOException) { } catch (System.Security.SecurityException) { } catch (System.UnauthorizedAccessException) { } return null; }
/// <summary> /// Sets or creates a registry value on a key. /// </summary> /// /// <param name="key"> /// The key to set or create the value on. /// </param> /// /// <param name="propertyName"> /// The name of the value to set or create. /// </param> /// /// <param name="value"> /// The new data for the value. /// </param> /// /// <param name="kind"> /// The RegistryValueKind of the value. /// </param> /// /// <param name="path"> /// The path to the key that the value is being set on. /// </param> /// /// <param name="writeResult"> /// If true, the value that is set will be written out. /// </param> /// private void SetRegistryValue( IRegistryWrapper key, string propertyName, object value, RegistryValueKind kind, string path, bool writeResult) { Dbg.Diagnostics.Assert( key != null, "Caller should have verified key"); string propertyNameToSet = GetPropertyName(propertyName); RegistryValueKind existingKind = RegistryValueKind.Unknown; // If user does not specify a kind: get the valuekind if the property // already exists if (kind == RegistryValueKind.Unknown) { existingKind = GetValueKindForProperty(key, propertyNameToSet); } // try to do a conversion based on the existing kind, if we // were able to retrieve one if (existingKind != RegistryValueKind.Unknown) { try { value = ConvertValueToKind(value, existingKind); kind = existingKind; } catch (InvalidCastException) { // failed attempt, we reset to unknown to let the // default conversion process take over existingKind = RegistryValueKind.Unknown; } } // set the kind as defined by the user if (existingKind == RegistryValueKind.Unknown) { // we use to kind passed in, either because we had // a valid one or because we failed to retrieve an existing kind to match if (kind == RegistryValueKind.Unknown) { // set the kind based on value if (value != null) { kind = GetValueKindFromObject(value); } else { // if no value and unknown kind, then default to empty string kind = RegistryValueKind.String; } } value = ConvertValueToKind(value, kind); } key.SetValue(propertyNameToSet, value, kind); if (writeResult) { // Now write out the value object newValue = key.GetValue(propertyNameToSet); WriteWrappedPropertyObject(newValue, propertyName, path); } } // SetRegistryValue
} // CopyProperty private void MoveProperty( IRegistryWrapper sourceKey, IRegistryWrapper destinationKey, string sourceProperty, string destinationProperty) { string realSourceProperty = GetPropertyName(sourceProperty); string realDestinationProperty = GetPropertyName(destinationProperty); try { // If sourceProperty and destinationProperty happens to be the same // then we shouldn't remove the property bool continueWithRemove = true; if (string.Equals(sourceKey.Name, destinationKey.Name, StringComparison.OrdinalIgnoreCase) && string.Equals(realSourceProperty, realDestinationProperty, StringComparison.OrdinalIgnoreCase)) { continueWithRemove = false; } // Move is implemented by copying the value and then deleting the original // Copy property will throw an exception if it fails CopyProperty( sourceKey, destinationKey, realSourceProperty, realDestinationProperty, false); // Delete sourceproperty only if it is not same as destination property if (continueWithRemove) { sourceKey.DeleteValue(realSourceProperty); } object newValue = destinationKey.GetValue(realDestinationProperty); WriteWrappedPropertyObject(newValue, destinationProperty, destinationKey.Name); } catch (System.IO.IOException ioException) { // An exception occurred while trying to get the key. Write // out the error. WriteError(new ErrorRecord(ioException, ioException.GetType().FullName, ErrorCategory.WriteError, sourceKey.Name)); return; } catch (System.Security.SecurityException securityException) { // An exception occurred while trying to get the key. Write // out the error. WriteError(new ErrorRecord(securityException, securityException.GetType().FullName, ErrorCategory.PermissionDenied, sourceKey.Name)); return; } 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, sourceKey.Name)); return; } } // MoveProperty
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