/// <summary> /// Harvest a registry key. /// </summary> /// <param name="registryKey">The registry key to harvest.</param> /// <param name="registryValues">The collected registry values.</param> private static void HarvestRegistryKey(RegistryKey registryKey, ArrayList registryValues) { // harvest the sub-keys foreach (string subKeyName in registryKey.GetSubKeyNames()) { using (RegistryKey subKey = registryKey.OpenSubKey(subKeyName)) HarvestRegistryKey(subKey, registryValues); } string[] parts = GetPathParts(registryKey.Name); RegistryRootType root; switch (parts[0]) { case "HKEY_CLASSES_ROOT": root = RegistryRootType.HKCR; break; case "HKEY_CURRENT_USER": root = RegistryRootType.HKCU; break; case "HKEY_LOCAL_MACHINE": root = RegistryRootType.HKLM; break; case "HKEY_USERS": root = RegistryRootType.HKU; break; default: throw new InvalidOperationException(string.Format("Unexpected root.")); } // harvest the values foreach (string valueName in registryKey.GetValueNames()) { var registryValue = new RegistryValue(); registryValue.Action = RegistryValue.ActionType.write; registryValue.Root = root; if (1 < parts.Length) { registryValue.Key = parts[1]; } if (null != valueName && 0 < valueName.Length) { registryValue.Name = valueName; } object value = registryKey.GetValue(valueName); if (value == null) { throw new InvalidOperationException(string.Format("Value is Null.")); } if (value is byte[]) // binary { var hexadecimalValue = new StringBuilder(); // convert the byte array to hexadecimal foreach (byte byteValue in (byte[])value) { hexadecimalValue.Append(byteValue.ToString("X2", CultureInfo.InvariantCulture.NumberFormat)); } registryValue.Type = RegistryValue.TypeType.binary; registryValue.Value = hexadecimalValue.ToString(); } else if (value is int) // integer { registryValue.Type = RegistryValue.TypeType.integer; registryValue.Value = ((int)value).ToString(CultureInfo.InvariantCulture); } else if (value is string[]) // multi-string { registryValue.Type = RegistryValue.TypeType.multiString; foreach (string multiStringValueContent in (string[])value) { var multiStringValue = new MultiStringValue(); multiStringValue.Content = multiStringValueContent; registryValue.AddChild(multiStringValue); } } else if (value is string) // string, expandable (there is no way to differentiate a string and expandable value in .NET 1.1) { registryValue.Type = RegistryValue.TypeType.@string; registryValue.Value = (string)value; } else { throw new InvalidOperationException(string.Format("Value is {0}.", value.GetType().AssemblyQualifiedName)); } registryValues.Add(registryValue); } }
protected void Load(RegistryCaptureKey key, IParentElement component) { // Iterate. foreach (RegistryCaptureKey subKey in key.SubKeys) { Load(subKey, component); } // Iterate over values. foreach (RegistryCaptureValue value in key.Values) { // Add the common information. RegistryValue registryValue = new RegistryValue(); if (!string.IsNullOrEmpty(value.Name)) { registryValue.Name = value.Name; } registryValue.Action = RegistryValue.ActionType.write; registryValue.Root = GetRegistryRoot(key.Root); registryValue.Key = key.RootRelativePath; // Add the type specific value. if (value is RegistryCaptureStringValue) { registryValue.Type = RegistryValue.TypeType.@string; registryValue.Value = GetValue((RegistryCaptureStringValue)value); } else if (value is RegistryCaptureDWordValue) { registryValue.Type = RegistryValue.TypeType.integer; registryValue.Value = GetValue((RegistryCaptureDWordValue)value); } else if (value is RegistryCaptureBinaryValue) { registryValue.Type = RegistryValue.TypeType.binary; registryValue.Value = GetValue((RegistryCaptureBinaryValue)value); } else if (value is RegistryCaptureExpandStringValue) { registryValue.Type = RegistryValue.TypeType.@string; registryValue.Value = GetValue((RegistryCaptureExpandStringValue)value); } else if (value is RegistryCaptureMultiStringValue) { registryValue.Type = RegistryValue.TypeType.multiString; foreach (string multiStringValueContent in ((RegistryCaptureMultiStringValue)value).Value) { MultiStringValue multiStringValue = new MultiStringValue(); multiStringValue.Content = multiStringValueContent; registryValue.AddChild(multiStringValue); } } else { registryValue.Type = RegistryValue.TypeType.@string; registryValue.Value = GetValue(value); } component.AddChild(registryValue); } }