protected override void WriteSettings(ICollection <IGrouping <Setting, Setting> > settings) { void DeleteObsoleteSettings(RegistryKey registryKey, IGrouping <Setting, Setting> group) { var obsoleteNames = registryKey .GetValueNames() .Where(x => RegistryPath.Parse(x).IsLike(group.Key.Name)) .ToList(); obsoleteNames.ForEach(registryKey.DeleteValue); } foreach (var group in settings) { var subKeyName = Path.Combine(_baseSubKeyName, group.Key.Name.Namespace); using (var subKey = _baseKey.OpenSubKey(subKeyName, true) ?? _baseKey.CreateSubKey(subKeyName) ?? throw new OpenOrCreateSubKeyException(_baseKey.Name, _baseSubKeyName, subKeyName) ) { DeleteObsoleteSettings(subKey, group); foreach (var setting in group) { if (!_registryValueKinds.TryGetValue(setting.Value.GetType(), out RegistryValueKind registryValueKind)) { throw new InvalidTypeException(setting.Value.GetType(), SupportedTypes); } var registryUrn = new RegistryPath(setting.Name); subKey.SetValue(registryUrn.StrongName, setting.Value, registryValueKind); } } } }
public override IEnumerable <Setting> ReadSettings(Setting setting) { var registryPath = new RegistryPath(setting.Name); var subKeyName = Path.Combine(_baseSubKeyName, registryPath.Namespace); using ( var subKey = _baseKey.OpenSubKey(subKeyName, false) ?? throw new OpenOrCreateSubKeyException(_baseKey.Name, _baseSubKeyName, subKeyName) ) { var valueNames = subKey.GetValueNames().Where(x => RegistryPath.Parse(x).IsLike(registryPath)); foreach (var valueName in valueNames) { yield return(new Setting { Name = SettingPath.Parse(valueName), Value = subKey.GetValue(valueName) }); } } }
public bool IsLike(RegistryPath registryPath) { return(WeakName.Equals(registryPath.WeakName, StringComparison.OrdinalIgnoreCase)); }