/** * Checks whether a registry value exists. * * @param root * HKEY_LOCAL_MACHINE, etc. * @param key * Registry key path. * @param value * Value name. * @return True if the value exists. */ public static boolean registryValueExists(HKEY root, String key, String value) { return registryValueExists(root, key, value, 0); }
/** * Set the file location only if it does not exist or has changed. * * @param eventMessageFile The message file location in the file system * @param categoryMessageFile The message file location in the file system * @param eventSourceKeyPath The registry path */ private void setVariableKeys(String eventMessageFile, String categoryMessageFile, String eventSourceKeyPath) { if (!Advapi32Util.registryValueExists(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, EVENT_MESSAGE_FILE) || !Advapi32Util .registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, EVENT_MESSAGE_FILE) .equalsIgnoreCase(eventMessageFile)) { Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, EVENT_MESSAGE_FILE, eventMessageFile); } if (!Advapi32Util.registryValueExists(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, CATEGORY_MESSAGE_FILE) || !Advapi32Util .registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, CATEGORY_MESSAGE_FILE) .equalsIgnoreCase(categoryMessageFile)) { Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, CATEGORY_MESSAGE_FILE, categoryMessageFile); } }
private void checkKeyExists( String rootKey, String keyPath, String keyName ) { try { WinReg.HKEY rootHKey = getHKey(rootKey); if (!Advapi32Util.registryValueExists(rootHKey, keyPath, keyName)) { throw new RegistryOperationsException("Registry key does not exist. " + getDescription(rootKey, keyPath, keyName)); } } catch (Win32Exception e) { throw new RegistryOperationsException("Registry key path does not exist. " + getDescription(rootKey, keyPath, keyName), e); } }