public IEnumerable <RegistryKey> openKey() { using RegistryKey baseKeyHandle = baseKey.openKey(); IEnumerable <RegistryKey> subKeys; if (isPatternLiteral(keyName)) { RegistryKey?subKey = baseKeyHandle.OpenSubKey(keyName !.ToString(), true); subKeys = subKey != null ? new[] { subKey } : Enumerable.Empty <RegistryKey>(); } else { // ReSharper disable once AccessToDisposedClosure - closure is evaluated by .ToList() before method returns and baseKeyHandle is disposed subKeys = baseKeyHandle.GetSubKeyNames() .Where(subkeyName => keyName?.IsMatch(subkeyName) ?? true) .Select(subkeyName => baseKeyHandle.OpenSubKey(subkeyName, true)) .Compact() .Where(subKey => displayName == null || (RegistryHelpers.getDisplayName(subKey) is { } displayNameValue&& displayName.IsMatch(displayNameValue))) .Compact() .ToList(); } return(subKeys); }