com.moyosoft.connector.registry
Class Registry

java.lang.Object
  |
  +--com.moyosoft.connector.registry.Registry

public final class Registry
extends java.lang.Object

Provides access to Windows Registry. This class has static methods used to get the root registry keys such as HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, etc.

Example source code:

// Load the native library:
System.loadLibrary("moyocore");

// Get the location of the Program Files folder from the registy:
String programFilesPath = Registry.getLocalMachineKey().getValue(
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
    "ProgramFilesDir").getString();

System.out.println("ProgramFilesDir = " + programFilesPath);
System.out.println();

// Create a new key:
RegistryKey registryKey = Registry.getLocalMachineKey().createKey("SOFTWARE\\TestMoyosoft");

try
{
    // Add some values:
    registryKey.setValue("Test value", "This is a test value");
    registryKey.setValue("Numeric value", 123);
    registryKey.setValue("Binary value", new byte[] {0x01, 0x02, 0x03});

    // Set the key's Default value:
    registryKey.setValue("", new RegistryValue(456));

    // Iterate through all values in this registry key:
    for(Iterator it = registryKey.getValues(); it.hasNext(); )
    {
        String valueName = (String) it.next();

        System.out.println("Value name: " + valueName);
        System.out.println("Value: " + registryKey.getValue(valueName));
        System.out.println();
    }

    // Iterate through all sub-keys in this registry key:
    for(Iterator it = registryKey.getSubKeys(); it.hasNext(); )
    {
        System.out.println("Key name: " + (String) it.next());
    }

    // Delete the previously created values:
    registryKey.deleteValue("Test value");
    registryKey.deleteValue("Numeric value");
    registryKey.deleteValue("Binary value");
    registryKey.deleteValue("");
}
finally
{
    // Close the registry key:
    registryKey.close();
}


Method Summary
static RegistryKey getCurrentConfigKey()
          Returns the registry key HKEY_CURRENT_CONFIG.
static RegistryKey getCurrentUserKey()
          Returns the registry key HKEY_CURRENT_USER.
static RegistryKey getLocalMachineKey()
          Returns the registry key HKEY_LOCAL_MACHINE.
static RegistryKey getUsersKey()
          Returns the registry key HKEY_USERS.
static RegistryKey openCurrentConfigKey(java.lang.String subKey)
          Opens the specified sub-key of the registry key HKEY_CURRENT_CONFIG.
static RegistryKey openCurrentUserKey(java.lang.String subKey)
          Opens the specified sub-key of the registry key HKEY_CURRENT_USER.
static RegistryKey openLocalMachineKey(java.lang.String subKey)
          Opens the specified sub-key of the registry key HKEY_LOCAL_MACHINE.
static RegistryKey openUsersKey(java.lang.String subKey)
          Opens the specified sub-key of the registry key HKEY_USERS.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getCurrentUserKey

public static RegistryKey getCurrentUserKey()
                                     throws RegistryException
Returns the registry key HKEY_CURRENT_USER.

Throws:
RegistryException - if any error occurs.

openCurrentUserKey

public static RegistryKey openCurrentUserKey(java.lang.String subKey)
                                      throws RegistryException
Opens the specified sub-key of the registry key HKEY_CURRENT_USER. If this method returns successfully, the returned RegistryKey object has to be closed using the method RegistryKey.close().

Parameters:
subKey - the sub-key to be opened.
Throws:
RegistryException - if any error occurs.

getUsersKey

public static RegistryKey getUsersKey()
                               throws RegistryException
Returns the registry key HKEY_USERS.

Throws:
RegistryException - if any error occurs.

openUsersKey

public static RegistryKey openUsersKey(java.lang.String subKey)
                                throws RegistryException
Opens the specified sub-key of the registry key HKEY_USERS. If this method returns successfully, the returned RegistryKey object has to be closed using the method RegistryKey.close().

Parameters:
subKey - the sub-key to be opened.
Throws:
RegistryException - if any error occurs.

getCurrentConfigKey

public static RegistryKey getCurrentConfigKey()
                                       throws RegistryException
Returns the registry key HKEY_CURRENT_CONFIG.

Throws:
RegistryException - if any error occurs.

openCurrentConfigKey

public static RegistryKey openCurrentConfigKey(java.lang.String subKey)
                                        throws RegistryException
Opens the specified sub-key of the registry key HKEY_CURRENT_CONFIG. If this method returns successfully, the returned RegistryKey object has to be closed using the method RegistryKey.close().

Parameters:
subKey - the sub-key to be opened.
Throws:
RegistryException - if any error occurs.

getLocalMachineKey

public static RegistryKey getLocalMachineKey()
                                      throws RegistryException
Returns the registry key HKEY_LOCAL_MACHINE.

Throws:
RegistryException - if any error occurs.

openLocalMachineKey

public static RegistryKey openLocalMachineKey(java.lang.String subKey)
                                       throws RegistryException
Opens the specified sub-key of the registry key HKEY_LOCAL_MACHINE. If this method returns successfully, the returned RegistryKey object has to be closed using the method RegistryKey.close().

Parameters:
subKey - the sub-key to be opened.
Throws:
RegistryException - if any error occurs.