1

I am trying to write a registry key in the following path in the registry:

HKEY_CURRENT_USER -> Software -> TestApp

I am currently doing:

    public static void main(String[] args)
{

    Preferences p = Preferences.userRoot().node("/HKEY_CURRENT_USER/Software/TestApp");
    p.put("TestKey", "TestValue");
}

But it writes it at HKEY_CURRENT_USER -> Software -> JavaSoft -> Prefs -> /H/K/E/Y_/C/U/R/R/E/N/T/_/U/S/E/R -> Software -> Test/App

How do I get it to follow the absolute path, and why is it putting extra slashes in?

Wolsie
  • 144
  • 1
  • 13

2 Answers2

1

You may take a look to this beautiful blog post about Read/Write the registry

I may draw your attention to this passage of the code:

/**
   * Write a value in a given key/value name
   * @param hkey
   * @param key
   * @param valueName
   * @param value
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   * @throws InvocationTargetException
   */
  public static void writeStringValue
    (int hkey, String key, String valueName, String value)
    throws IllegalArgumentException, IllegalAccessException,
    InvocationTargetException
  {
    if (hkey == HKEY_LOCAL_MACHINE) {
      writeStringValue(systemRoot, hkey, key, valueName, value);
    }
    else if (hkey == HKEY_CURRENT_USER) {
      writeStringValue(userRoot, hkey, key, valueName, value);
    }
    else {
      throw new IllegalArgumentException("hkey=" + hkey);
    }
  }

I think this solution is very elegant in onder to manage read/write operations against the registry.

abarisone
  • 3,707
  • 11
  • 35
  • 54
0

How do I get it to follow the absolute path

You can use reflection to access the private methods in the java.util.prefs.Preferences class. See this answer for details: https://stackoverflow.com/a/6163701/6256127

However, I don't recommend using this approach as it may break at any time.

why is it putting extra slashes in?

Have a look at this answer: https://stackoverflow.com/a/23632932/6256127

Registry-Keys are case-preserving, but case-insensitive. For example if you have a key "Rbi" you cant make another key named "RBi". The case is saved but ignored. Sun's solution for case-sensitivity was to add slashes to the key.

Community
  • 1
  • 1