Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ini4j编辑Windows注册表_Java_Registry_Ini4j - Fatal编程技术网

Java 使用ini4j编辑Windows注册表

Java 使用ini4j编辑Windows注册表,java,registry,ini4j,Java,Registry,Ini4j,我目前正在开发一个java程序,我需要读/写注册表。我研究了几个API来实现这一点,发现了ini4j()。我还需要编辑ini文件,所以我喜欢这个解决方案,因为它既可以编辑ini文件,也可以编辑ini文件。我很好奇是否有人在这种情况下尝试过ini4j?我找到了一个更好的解决方案,可以在不需要ini4j或向命令行传递参数的情况下读取/写入注册表。我在我的程序中大量使用JNA,因此我认为使用本机库调用会更容易,而不是包括一个额外的库来为我执行此操作。下面是我的项目中的一个例子,我在注册表中搜索特定的键

我目前正在开发一个java程序,我需要读/写注册表。我研究了几个API来实现这一点,发现了ini4j()。我还需要编辑ini文件,所以我喜欢这个解决方案,因为它既可以编辑ini文件,也可以编辑ini文件。我很好奇是否有人在这种情况下尝试过ini4j?

我找到了一个更好的解决方案,可以在不需要ini4j或向命令行传递参数的情况下读取/写入注册表。我在我的程序中大量使用JNA,因此我认为使用本机库调用会更容易,而不是包括一个额外的库来为我执行此操作。下面是我的项目中的一个例子,我在注册表中搜索特定的键。特定密钥还取决于操作系统是x64还是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;
         }
    }  
public静态字符串GetUninstallerPath(){
试一试{
//如果(logger.isinfo已启用)logger.Info(“GetUninstallerPath-begin”);
字符串uninstallerPath=null;
试一试{
字符串vncDisplayName=“UltraVNC”;
String subkey32=“软件\\Microsoft\\Windows\\CurrentVersion\\Uninstall”;
String subkey64=“软件\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall”;
布尔值is64Bit=Platform.is64Bit();
字符串[]键;
if(64位){
key=Advapi32Util.registryGetKeys(WinReg.HKEY\u LOCAL\u机器,
子键64);
}否则{
key=Advapi32Util.registryGetKeys(WinReg.HKEY\u LOCAL\u机器,
子键32);
}
if(key!=null){
for(字符串nextSubkeyName:key){
TreeMap subKey=Advapi32Util.registryGetValues(
WinReg.HKEY_本地_机器,
子键64+“\\”+nextSubkeyName);
Object value=subKey.get(“DisplayName”);
对象路径=空;
if(值!=null){
if(value.toString().startsWith(vncDisplayName)){
path=subKey.get(“卸载字符串”);
if(路径!=null){
卸载路径=path.toString().trim();
}
}
}
}
}
}
捕获(例外情况除外){
System.err.println(例如getMessage());
}
返回卸载路径;
}
}  

我最初使用对象来存储键值,因为我一直在获取NullPointerException。请随意提供另一个解决方案

我找到了一个更好的解决方案,可以在不需要ini4j或向命令行传递参数的情况下读取/写入注册表。我在我的程序中大量使用JNA,因此我认为使用本机库调用会更容易,而不是包括一个额外的库来为我执行此操作。下面是我的项目中的一个例子,我在注册表中搜索特定的键。特定密钥还取决于操作系统是x64还是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;
         }
    }  
public静态字符串GetUninstallerPath(){
试一试{
//如果(logger.isinfo已启用)logger.Info(“GetUninstallerPath-begin”);
字符串uninstallerPath=null;
试一试{
字符串vncDisplayName=“UltraVNC”;
String subkey32=“软件\\Microsoft\\Windows\\CurrentVersion\\Uninstall”;
String subkey64=“软件\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall”;
布尔值is64Bit=Platform.is64Bit();
字符串[]键;
if(64位){
key=Advapi32Util.registryGetKeys(WinReg.HKEY\u LOCAL\u机器,
子键64);
}否则{
key=Advapi32Util.registryGetKeys(WinReg.HKEY\u LOCAL\u机器,
子键32);
}
if(key!=null){
for(字符串nextSubkeyName:key){
TreeMap subKey=Advapi32Util.registryGetValues(
WinReg.HKEY_本地_机器,
子键64+“\\”+nextSubkeyName);
Object value=subKey.get(“DisplayName”);
对象路径=空;
if(值!=null){
if(value.toString().startsWith(vncDisplayName)){
path=subKey.get(“卸载字符串”);
if(路径!=null){
卸载路径=path.toString().trim();
}
}
}
}
}
}
捕获(例外情况除外){
System.err.println(例如getMessage());
}
返回卸载路径;
}
}  

我最初使用对象来存储键值,因为我一直在获取NullPointerException。请随意提供另一个解决方案

不幸的是,您使用Platform.is64Bit()进行的64位测试并没有达到您认为的效果

它告诉您JVM是32位还是64位,而不是Windows是32位还是64位

您的代码似乎能按预期工作的唯一原因是Windows注册表重定向器为您处理了所涉及的“魔法”(访问正确的注册表项)

当您的代码在64位Windows平台上的32位JVM上运行时。is64Bit()返回false,并且您正在使用subkey32(即“Software\Microsoft\Windows\CurrentVersion\Uninstall”)

不幸的是,我犯了和你一样的错误,发布了一个