// returns false in case of FileNotFound error, otherwise throws exception internal bool TryApplyOperation(KeyDisposition disposition, string subKeyName, KeySecurity samDesired, KeyImplOperation operation, Win32Api.RegWow64Options wowOptions = Win32Api.RegWow64Options.None) { // None is passed from Get/Set/Enum operations which does not allow user to pass wow options // in such case we take it from the current identity if (wowOptions == Win32Api.RegWow64Options.None) { wowOptions = identity_.GetWow64Mode(); } KeyImplHolder subKey; if (!TryGetSubKey(disposition, subKeyName, samDesired, wowOptions, out subKey)) { return(false); } using (subKey) { // TODO: almost always the operation needs subKey name, at least for logging // so it is constructed second time there. // Better to pass it from here. try { if (!operation(subKey.GetKeyImpl())) { return(false); } } catch (FileNotFoundException) { // TODO: make all operations return false in case of FileNotFoundException // so that this catch can be removed. return(false); } } return(true); }
public static bool TryOpen(KeyIdentity identity, KeySecurity samDesired, out WindowsKey openedImpl) { if (identity.GetRegPath() == null) { // This works fine because baseKey is always one of the predefined // registry keys, so its handle does not need to be duplicated // or closed openedImpl = new WindowsKey(identity.BaseKey, samDesired); return(true); } IntPtr handle; int result = Win32Api.RegOpenKeyEx(identity.BaseKey, identity.GetRegPath(), 0, samDesired.Value | (Win32Api.KeySecurity)identity.GetWow64Mode(), out handle); openedImpl = null; if (!Win32Exception.CheckIfFoundAndNoError(result)) { return(false); } openedImpl = new WindowsKey(handle, samDesired); return(true); }