Search code examples
javaregistryini4j

Using ini4j to edit Windows registry


Im currently working on a java program and I need to read/write to the registry. i've looked at several API's to do this and I found ini4j (ini4j Project Page). I also need to edit ini files so I like this solution because it does both. I'm curious if anybody has tried ini4j in this type of scenario?


Solution

  • I found a better solution for reading/writing to the registry without the need for ini4j or passing arguments to the command line. I use JNA quite a lot in my program so I figured that it would be easier to use native library calls instead of including an additional library to do this for me. Here is an example from my project were I search through the registry looking for a specific key. The specific key is also dependent on whether or not the OS is x64 or x86.

       public static String GetUninstallerPath() {
            try {
                //if (logger.IsInfoEnabled) logger.Info("GetUninstallerPath - begin");
    
                String uninstallerPath = null;
    
                try {
                    String vncDisplayName = "UltraVNC";
                    String subkey32 = "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
                    String subkey64 = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
    
                    boolean is64Bit = Platform.is64Bit();
                    String[] key;
                    if (is64Bit) {
                        key = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE,
                                subkey64);
                    } else {
                        key = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE,
                                subkey32);
                    }
    
                    if (key != null) {
                        for (String nextSubkeyName : key) {
    
                            TreeMap<String, Object> subKey = Advapi32Util.registryGetValues(
                                    WinReg.HKEY_LOCAL_MACHINE,
                                    subkey64 + "\\" + nextSubkeyName);
                            Object value = subKey.get("DisplayName");
                            Object path = null;
                            if (value != null) {
                                if (value.toString().startsWith(vncDisplayName)) {
                                    path = subKey.get("UninstallString");
                                    if (path != null) {
                                        uninstallerPath = path.toString().trim();
                                    }
                                }
                            }
    
                        }
    
                    }
    
                }
                catch (Exception ex) {
                    System.err.println(ex.getMessage());
    
                }
    
                return uninstallerPath;
             }
        }  
    

    I use objects to initially store the key values because I kept getting NullPointerExceptions. Feel free to provide another solution.