public void OpenSubKey_and_CreateSubKey_should_accept_nested_paths() { var registry = new FakeRegistry(); using (IWin32RegistryKey hkcu = registry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64)) using (IWin32RegistryKey subKey = hkcu.CreateSubKey(@"a\b\c")) { hkcu.OpenSubKey(@"a\b\c").Should().NotBeNull(); hkcu.OpenSubKey(@"a\b").Should().NotBeNull(); hkcu.OpenSubKey(@"a").Should().NotBeNull(); } }
//// =========================================================================================================== //// Methods //// =========================================================================================================== /// <summary> /// Reads a value from the Windows registryKey. /// </summary> /// <typeparam name="T">The type of value that is read (<see cref="RegistryReadValueCommand{T}"/>).</typeparam> /// <param name="command">The command to execute.</param> /// <param name="logger">The logger to use.</param> /// <returns>A response indicating success and the read value, or failure with error details.</returns> public IServiceCommandResponse ExecuteRead <T>(RegistryReadValueCommand <T> command, ILogger logger) { Param.VerifyNotNull(command, nameof(command)); Param.VerifyNotNull(logger, nameof(logger)); ServiceCommandResponse response; try { var registryHive = (RegistryHive)Enum.Parse(typeof(RegistryHive), command.BaseKey.ToString()); using (IWin32RegistryKey baseKey = _registry.OpenBaseKey(registryHive, RegistryView.Registry64)) using (IWin32RegistryKey subKey = baseKey.OpenSubKey(command.Key, writable: false)) { object value = subKey?.GetValue(command.ValueName, command.DefaultValue) ?? command.DefaultValue; response = ServiceCommandResponse.Create(command.CommandName, value); } } catch (Exception e) { response = ServiceCommandResponse.CreateError( command.CommandName, ServiceErrorInfo.RegistryReadError( RegistryPath.HiveToWin32Name(RegistryPath.BaseKeyToHive(command.BaseKey)), command.Key, command.ValueName, e)); logger.LogError(response.ErrorMessage); } return(response); }
public void OpenSubKey_should_return_null_if_the_key_does_not_exist() { var registry = new FakeRegistry(); using (IWin32RegistryKey hkcu = registry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64)) { hkcu.OpenSubKey("NotThere").Should().BeNull(); } }
public void CreateSubKey_should_create_a_key_if_it_does_not_exist() { var registry = new FakeRegistry(); using (IWin32RegistryKey hkcu = registry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64)) using (hkcu.CreateSubKey("SubKey")) { hkcu.OpenSubKey("SubKey").Should().NotBeNull(); } }
public void OpenSubKey_should_throw_ObjectDisposedException_if_closed() { var registry = new FakeRegistry(); using (IWin32RegistryKey hkcu = registry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64)) { IWin32RegistryKey subKey = hkcu.CreateSubKey("SubKey", writable: false); subKey.Dispose(); Action action = () => subKey.OpenSubKey("ValueName"); action.Should().ThrowExactly <ObjectDisposedException>(); } }