Class WinRegistry


  • public class WinRegistry
    extends java.lang.Object
    Collection of static methods to access the Microsoft Windows registry.

    If RegistryRoot.HKEY_CURRENT_USER is specified, the correct result for the original user is returned even if executed in the elevated helper.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  WinRegistry.ExpandString
      Class to represent Strings with type REG_EXPAND_SZ.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean createKey​(RegistryRoot root, java.lang.String keyName)
      Creates a registry key.
      static boolean createKey​(RegistryRoot root, java.lang.String keyName, RegistryView registryView)
      Creates a registry key.
      static void deleteKey​(RegistryRoot root, java.lang.String keyName, boolean onlyIfEmpty)
      Deletes a registry key.
      static void deleteKey​(RegistryRoot root, java.lang.String keyName, boolean onlyIfEmpty, RegistryView registryView)
      Deletes a registry key.
      static void deleteValue​(RegistryRoot root, java.lang.String keyName, java.lang.String valueName)
      Deletes a registry value.
      static void deleteValue​(RegistryRoot root, java.lang.String keyName, java.lang.String valueName, RegistryView registryView)
      Deletes a registry value.
      static java.lang.String[] getSubKeyNames​(RegistryRoot root, java.lang.String keyName)
      Retrieves the sub keys of a registry key.
      static java.lang.String[] getSubKeyNames​(RegistryRoot root, java.lang.String keyName, RegistryView registryView)
      Retrieves the sub keys of a registry key.
      static java.lang.Object getValue​(RegistryRoot root, java.lang.String keyName, java.lang.String valueName)
      Retrieves a registry value.
      static java.lang.Object getValue​(RegistryRoot root, java.lang.String keyName, java.lang.String valueName, RegistryView registryView)
      Retrieves a registry value.
      static java.lang.String[] getValueNames​(RegistryRoot root, java.lang.String keyName)
      Retrieves the exiting value names of a registry key.
      static java.lang.String[] getValueNames​(RegistryRoot root, java.lang.String keyName, RegistryView registryView)
      Retrieves the exiting value names of a registry key.
      static boolean keyExists​(RegistryRoot root, java.lang.String keyName)
      Checks if a registry key exists.
      static boolean keyExists​(RegistryRoot root, java.lang.String keyName, RegistryView registryView)
      Checks if a registry key exists.
      static boolean restoreKey​(RegistryRoot root, java.lang.String keyName, java.lang.String fileName)
      Restore a sub-tree of the registry from a file.
      static boolean restoreKey​(RegistryRoot root, java.lang.String keyName, java.lang.String fileName, RegistryView registryView)
      Restore a sub-tree of the registry from a file.
      static boolean saveKey​(RegistryRoot root, java.lang.String keyName, java.lang.String fileName)
      Save a sub-tree of the registry to a file.
      static boolean saveKey​(RegistryRoot root, java.lang.String keyName, java.lang.String fileName, RegistryView registryView)
      Save a sub-tree of the registry to a file.
      static boolean setValue​(RegistryRoot root, java.lang.String keyName, java.lang.String valueName, java.lang.Object value)
      Sets a registry value.
      static boolean setValue​(RegistryRoot root, java.lang.String keyName, java.lang.String valueName, java.lang.Object value, RegistryView registryView)
      Sets a registry value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • keyExists

        public static boolean keyExists​(RegistryRoot root,
                                        java.lang.String keyName)
        Checks if a registry key exists.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        Returns:
        true if the key exists.
      • createKey

        public static boolean createKey​(RegistryRoot root,
                                        java.lang.String keyName)
        Creates a registry key.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        Returns:
        true if successful.
      • getValue

        public static java.lang.Object getValue​(RegistryRoot root,
                                                java.lang.String keyName,
                                                java.lang.String valueName)
        Retrieves a registry value. The class of the returned object is determined by the value's type.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        valueName - the name of the registry value.
        Returns:
        the requested value. Returns null, if the value doesn't exist. Otherwise it is an instance of one of the following classes: String, Integer, String[], byte[], WinRegistry.ExpandString.
      • setValue

        public static boolean setValue​(RegistryRoot root,
                                       java.lang.String keyName,
                                       java.lang.String valueName,
                                       java.lang.Object value)
        Sets a registry value. The type is determined by the class of the value.
        Parameters:
        root - one of the RegistryRoot.* constants
        keyName - the name of the key without a leading backslash.
        valueName - the name of the registry value.
        value - an instance of one of the following classes: String, Integer, String[], byte[], WinRegistry.ExpandString.
        Returns:
        true if successful.
      • deleteValue

        public static void deleteValue​(RegistryRoot root,
                                       java.lang.String keyName,
                                       java.lang.String valueName)
        Deletes a registry value.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        valueName - the name of the registry value.
      • deleteKey

        public static void deleteKey​(RegistryRoot root,
                                     java.lang.String keyName,
                                     boolean onlyIfEmpty)
        Deletes a registry key.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        onlyIfEmpty - if true, the method will delete the key only if the key contains no values and no sub-key.
      • getSubKeyNames

        public static java.lang.String[] getSubKeyNames​(RegistryRoot root,
                                                        java.lang.String keyName)
        Retrieves the sub keys of a registry key.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        Returns:
        an array with the sub keys' names. Returns an empty array if there are no sub keys and null if the key doesn't exist or if an error occurs.
      • getValueNames

        public static java.lang.String[] getValueNames​(RegistryRoot root,
                                                       java.lang.String keyName)
        Retrieves the exiting value names of a registry key.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        Returns:
        an array with the values' names. Returns an empty array if there are no values and null if the key doesn't exist or if an error occurs.
      • saveKey

        public static boolean saveKey​(RegistryRoot root,
                                      java.lang.String keyName,
                                      java.lang.String fileName)
        Save a sub-tree of the registry to a file. The sub-tree can be restored with restoreKey.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash. This is the root of the saved sub-tree.
        fileName - the file to which the sub-tree should be saved.
        Returns:
        whether the operation was successful or not.
        See Also:
        restoreKey(RegistryRoot, String, String)
      • restoreKey

        public static boolean restoreKey​(RegistryRoot root,
                                         java.lang.String keyName,
                                         java.lang.String fileName)
        Restore a sub-tree of the registry from a file. This method can restore files saved with the saveKey method.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash. This is the key to which the root of the saved sub-tree will be restored.
        fileName - the file from which the saved sub-tree should be read.
        Returns:
        whether the operation was successful or not.
        See Also:
        saveKey(RegistryRoot, String, String)
      • keyExists

        public static boolean keyExists​(RegistryRoot root,
                                        java.lang.String keyName,
                                        RegistryView registryView)
        Checks if a registry key exists.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        registryView - the registry view used on 64-bit systems.
        Returns:
        true if the key exists.
      • createKey

        public static boolean createKey​(RegistryRoot root,
                                        java.lang.String keyName,
                                        RegistryView registryView)
        Creates a registry key.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        registryView - the registry view used on 64-bit systems.
        Returns:
        true if successful.
      • getValue

        public static java.lang.Object getValue​(RegistryRoot root,
                                                java.lang.String keyName,
                                                java.lang.String valueName,
                                                RegistryView registryView)
        Retrieves a registry value. The class of the returned object is determined by the value's type.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        valueName - the name of the registry value.
        registryView - the registry view used on 64-bit systems.
        Returns:
        the requested value. Returns null, if the value doesn't exist. Otherwise it is an instance of one of the following classes: String, Integer, String[], byte[], WinRegistry.ExpandString.
      • setValue

        public static boolean setValue​(RegistryRoot root,
                                       java.lang.String keyName,
                                       java.lang.String valueName,
                                       java.lang.Object value,
                                       RegistryView registryView)
        Sets a registry value. The type is determined by the class of the value.
        Parameters:
        root - one of the RegistryRoot.* constants
        keyName - the name of the key without a leading backslash.
        valueName - the name of the registry value.
        value - an instance of one of the following classes: String, Integer, String[], byte[], WinRegistry.ExpandString.
        registryView - the registry view used on 64-bit systems.
        Returns:
        true if successful.
      • deleteValue

        public static void deleteValue​(RegistryRoot root,
                                       java.lang.String keyName,
                                       java.lang.String valueName,
                                       RegistryView registryView)
        Deletes a registry value.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        valueName - the name of the registry value.
        registryView - the registry view used on 64-bit systems.
      • deleteKey

        public static void deleteKey​(RegistryRoot root,
                                     java.lang.String keyName,
                                     boolean onlyIfEmpty,
                                     RegistryView registryView)
        Deletes a registry key.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        onlyIfEmpty - if true, the method will delete the key only if the key contains no values and no sub-key.
        registryView - the registry view used on 64-bit systems.
      • getSubKeyNames

        public static java.lang.String[] getSubKeyNames​(RegistryRoot root,
                                                        java.lang.String keyName,
                                                        RegistryView registryView)
        Retrieves the sub keys of a registry key.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        registryView - the registry view used on 64-bit systems.
        Returns:
        an array with the sub keys' names. Returns an empty array if there are no sub keys and null if the key doesn't exist or if an error occurs.
      • getValueNames

        public static java.lang.String[] getValueNames​(RegistryRoot root,
                                                       java.lang.String keyName,
                                                       RegistryView registryView)
        Retrieves the exiting value names of a registry key.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash.
        registryView - the registry view used on 64-bit systems. null if the key doesn't exist or if an error occurs.
        Returns:
        an array with the values' names. Returns an empty array if there are no values and
      • saveKey

        public static boolean saveKey​(RegistryRoot root,
                                      java.lang.String keyName,
                                      java.lang.String fileName,
                                      RegistryView registryView)
        Save a sub-tree of the registry to a file. The sub-tree can be restored with restoreKey.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash. This is the root of the saved sub-tree.
        fileName - the file to which the sub-tree should be saved.
        registryView - the registry view used on 64-bit systems.
        Returns:
        whether the operation was successful or not.
        See Also:
        restoreKey(RegistryRoot, String, String)
      • restoreKey

        public static boolean restoreKey​(RegistryRoot root,
                                         java.lang.String keyName,
                                         java.lang.String fileName,
                                         RegistryView registryView)
        Restore a sub-tree of the registry from a file. This method can restore files saved with the saveKey method.
        Parameters:
        root - one of the RegistryRoot.* constants.
        keyName - the name of the key without a leading backslash. This is the key to which the root of the saved sub-tree will be restored.
        fileName - the file from which the saved sub-tree should be read.
        registryView - the registry view used on 64-bit systems.
        Returns:
        whether the operation was successful or not.
        See Also:
        saveKey(RegistryRoot, String, String)