protected override async Task <int> ExecuteOnExtractedPackage(string directoryPath) { var command = new SetRegistryKey { RegistryKey = this.Verb.RegistryKey }; var target = RegistryPathConverter.ToCanonicalRegistryPath(command.RegistryKey); try { await new SetRegistryKeyExecutor(directoryPath).Execute(command).ConfigureAwait(false); } catch (Exception e) { await this.Console.WriteError($"Could not set registry key {target.Item1}\\{target.Item2}.").ConfigureAwait(false); await this.Console.WriteError(e.Message).ConfigureAwait(false); return(StandardExitCodes.ErrorGeneric); } await this.Console.WriteSuccess($"Registry key {target.Item1}\\{target.Item2} has been set."); return(StandardExitCodes.ErrorSuccess); }
protected override async Task <int> ExecuteOnExtractedPackage(string directoryPath) { var command = new DeleteRegistryValue { RegistryKey = this.Verb.RegistryKey, RegistryValueName = this.Verb.RegistryValueName }; var target = RegistryPathConverter.ToCanonicalRegistryPath(command.RegistryKey); try { await new DeleteRegistryValueExecutor(directoryPath).Execute(command).ConfigureAwait(false); } catch (Exception e) { await this.Console.WriteError($"Registry value {this.Verb.RegistryValueName} could not be removed from '{target.Item1}\\{target.Item2}'.").ConfigureAwait(false); await this.Console.WriteError(e.Message).ConfigureAwait(false); return(StandardExitCodes.ErrorGeneric); } await this.Console.WriteSuccess($"Registry value {this.Verb.RegistryValueName} has been removed from '{target.Item1}\\{target.Item2}."); return(StandardExitCodes.ErrorSuccess); }
private void WriteKeyToFacade(RegistryKey registryKey) { var data = RegistryPathConverter.ToCanonicalRegistryPath(registryKey.Name); msixRegistryFacade.Add(new RegistryEntry { Key = data.Item2, Root = data.Item1 }); foreach (var valueName in registryKey.GetValueNames()) { ValueType valueType; switch (registryKey.GetValueKind(valueName)) { case RegistryValueKind.None: case RegistryValueKind.Unknown: valueType = ValueType.None; break; case RegistryValueKind.ExpandString: valueType = ValueType.Expandable; break; case RegistryValueKind.Binary: valueType = ValueType.Binary; break; case RegistryValueKind.DWord: valueType = ValueType.DWord; break; case RegistryValueKind.MultiString: valueType = ValueType.Multi; break; case RegistryValueKind.QWord: valueType = ValueType.QWord; break; default: valueType = ValueType.String; break; } msixRegistryFacade.Add(new RegistryEntry { Key = data.Item2, Value = registryKey.GetValue(valueName), Name = valueName, Root = data.Item1, Type = valueType }); } }
public override async Task Execute(SetRegistryKey command, CancellationToken cancellationToken = default) { var converter = new MsixRegistryFileWriter(this.Directory.FullName); converter.WriteKey(command.RegistryKey); await converter.Flush().ConfigureAwait(false); var target = RegistryPathConverter.ToCanonicalRegistryPath(command.RegistryKey); Logger.Info($"Registry key {target.Item1}\\{target.Item2} has been set."); }
public override async Task Execute(SetRegistryValue command, CancellationToken cancellationToken = default) { var converter = new MsixRegistryFileWriter(this.Directory.FullName); converter.WriteValue(command.RegistryKey, command.RegistryValueName, command.ValueType, command.Value); await converter.Flush().ConfigureAwait(false); var target = RegistryPathConverter.ToCanonicalRegistryPath(command.RegistryKey); Logger.Info($"Registry value {command.Value} = '{command.Value}' ({command.ValueType}) has been set in '{target.Item1}\\{target.Item2}'."); }
public override async Task Execute(DeleteRegistryValue command, CancellationToken cancellationToken = default) { var target = RegistryPathConverter.ToCanonicalRegistryPath(command.RegistryKey); Logger.Info($"Removing value {command.RegistryValueName} from key {target.Item1}\\{target.Item2}."); var regFileWriter = new MsixRegistryFileWriter(this.Directory.FullName); regFileWriter.WriteValue(command.RegistryKey, command.RegistryValueName, ValueType.None, null); if (!await regFileWriter.Flush().ConfigureAwait(false)) { Logger.Warn($"No changes have been applied. Registry key {target.Item1}\\{target.Item2} does not exist."); } }
public void ImportLocalRegistryKey(string registryPath) { if (registryPath.IndexOf('\\') == -1) { throw new ArgumentException("The path must be a registry path.", nameof(registryPath)); } var data = RegistryPathConverter.ToCanonicalRegistryPath(registryPath); RegistryKey sourceRegistryHive; switch (data.Item1) { case RegistryRoot.HKEY_CLASSES_ROOT: sourceRegistryHive = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Default); break; case RegistryRoot.HKEY_LOCAL_MACHINE: sourceRegistryHive = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default); break; case RegistryRoot.HKEY_CURRENT_USER: sourceRegistryHive = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default); break; default: throw new NotSupportedException($"Registry hive {data.Item1} is not supported."); } using (sourceRegistryHive) { var sourcePath = registryPath.Substring(registryPath.IndexOf('\\') + 1); using var sourceRegistryKey = sourceRegistryHive.OpenSubKey(sourcePath); this.WriteKeyToFacade(sourceRegistryKey); } }
public async Task <bool> Flush() { var rawReader = new RawReader(); IRegHive hiveRegistry = null; IRegHive hiveUser = null; IRegHive hiveUserClasses = null; if (File.Exists(this.fileRegistry)) { hiveRegistry = await rawReader.Open(this.fileRegistry).ConfigureAwait(false); } if (File.Exists(this.fileUser)) { hiveUser = await rawReader.Open(this.fileUser).ConfigureAwait(false); } if (File.Exists(this.fileUserClasses)) { hiveUserClasses = await rawReader.Open(this.fileUserClasses).ConfigureAwait(false); } if (hiveUserClasses == null && hiveRegistry == null && hiveUser == null) { return(false); } var foundAnythingRegistry = false; var foundAnythingUser = false; var foundAnythingUserClasses = false; using (hiveRegistry) { using (hiveUser) { using (hiveUserClasses) { foreach (var key in this.deletedKeys) { var msix = RegistryPathConverter.ToMsixRegistryPath(key); if (hiveRegistry != null) { using var foundKey = this.GetExistingRegistryKey(hiveRegistry.Root, msix); if (foundKey != null) { foundAnythingRegistry = true; RemoveRecurse(foundKey); } } if (hiveUser != null) { using var foundKey = this.GetExistingRegistryKey(hiveUser.Root, msix); if (foundKey != null) { foundAnythingUser = true; RemoveRecurse(foundKey); } } if (hiveUserClasses != null) { using var foundKey = this.GetExistingRegistryKey(hiveUserClasses.Root, msix); if (foundKey != null) { foundAnythingUserClasses = true; RemoveRecurse(foundKey); } } } if (foundAnythingRegistry) { await hiveRegistry.Save(this.fileRegistry).ConfigureAwait(false); } if (foundAnythingUser) { await hiveUser.Save(this.fileUser).ConfigureAwait(false); } if (foundAnythingUserClasses) { await hiveUserClasses.Save(this.fileUserClasses).ConfigureAwait(false); } } } } this.deletedKeys.Clear(); return(foundAnythingRegistry || foundAnythingUser || foundAnythingUserClasses); }
private void CommitEntries(IRegKey rootKey, IEnumerable <RegistryEntry> entries) { foreach (var entry in entries) { var actualKey = RegistryPathConverter.ToMsixRegistryPath(entry); if (entry.Name != null && entry.Value == null) { var lastUnit = actualKey.Substring(0, actualKey.LastIndexOf('\\')); var parent = this.GetExistingRegistryKey(rootKey, lastUnit); if (parent == null) { // don't create a key if we are just about to remove the value... continue; } } var key = this.GetOrCreateRegistryKey(rootKey, actualKey); if (entry.Name != null || entry.Value != null) { if (!string.IsNullOrEmpty(entry.Name)) { if (entry.Value == null) { key.RemoveValue(entry.Name); } else { switch (entry.Type) { case ValueType.Default: break; case ValueType.String: key.SetValue(entry.Name, RegistryValueConverter.ToMsixFormat((string)entry.Value)); break; case ValueType.DWord: var val = (long)Convert.ChangeType(entry.Value, typeof(long)); if (val > int.MaxValue) { key.SetValue(entry.Name, val); } else { key.SetValue(entry.Name, (int)val); } break; case ValueType.QWord: key.SetValue(entry.Name, (long)Convert.ChangeType(entry.Value, typeof(long))); break; case ValueType.Multi: key.SetValue(entry.Name, RegistryValueConverter.ToMsixFormat((string[])entry.Value)); break; case ValueType.Expandable: key.SetValue(entry.Name, RegistryValueConverter.ToMsixFormat((string)entry.Value)); break; case ValueType.Binary: key.SetValue(entry.Name, (byte[])entry.Value); break; case ValueType.DWordBigEndian: key.SetValue(entry.Name, (int)Convert.ChangeType(entry.Value, typeof(int))); break; } } } else if (entry.Value is string sourceStringValue) { key.SetValue(string.Empty, sourceStringValue); } } } }
public void WriteKey(string registryKey) { var converted = RegistryPathConverter.ToCanonicalRegistryPath(registryKey); this.WriteKey(converted.Item1, converted.Item2); }
public void WriteValue(string registryKey, string name, ValueType type, object value) { var converted = RegistryPathConverter.ToCanonicalRegistryPath(registryKey); this.WriteValue(converted.Item1, converted.Item2, name, type, value); }
protected override async Task <int> ExecuteOnExtractedPackage(string directoryPath) { var command = new SetRegistryValue { RegistryKey = this.Verb.RegistryKey, RegistryValueName = this.Verb.RegistryValueName, ValueType = Enum.Parse <ValueType>(this.Verb.ValueType, true) }; switch (command.ValueType) { case ValueType.None: case ValueType.Default: case ValueType.String: case ValueType.Expandable: command.Value = RawRegistryValueConverter.GetStringFromString(this.Verb.Value); break; case ValueType.DWord: unchecked { // Note: this conversion is by-design because .NET API for registry expects signed values. command.Value = (int)RawRegistryValueConverter.GetDWordFromString(this.Verb.Value); break; } case ValueType.QWord: unchecked { // Note: this conversion is by-design because .NET API for registry expects signed values. command.Value = (long)RawRegistryValueConverter.GetQWordFromString(this.Verb.Value); break; } case ValueType.Multi: command.Value = RawRegistryValueConverter.GetMultiValueFromString(this.Verb.Value); break; case ValueType.Binary: command.Value = RawRegistryValueConverter.GetByteArrayFromString(this.Verb.Value); break; default: return(StandardExitCodes.ErrorGeneric); } var target = RegistryPathConverter.ToCanonicalRegistryPath(command.RegistryKey); try { await new SetRegistryValueExecutor(directoryPath).Execute(command).ConfigureAwait(false); } catch (Exception e) { await this.Console.WriteError($"Could not set registry value '{this.Verb.RegistryValueName}' to '{this.Verb.Value}' ({command.ValueType:G}) in '{target.Item1}\\{target.Item2}'.").ConfigureAwait(false); await this.Console.WriteError(e.Message).ConfigureAwait(false); return(StandardExitCodes.ErrorGeneric); } await this.Console.WriteSuccess($"Registry value '{this.Verb.RegistryValueName}' has been set to '{this.Verb.Value}' ({command.ValueType:G}) in '{target.Item1}\\{target.Item2}'."); return(StandardExitCodes.ErrorSuccess); }