注册表检查路径/密钥是否存在

时间:2016-10-06 12:20:35

标签: java registry

嘿伙计们,快速愚蠢的问题......

我收到了这段代码:

String arch = System.getenv("PROCESSOR_ARCHITECTURE");
String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");
String realArch = arch.endsWith("64") || wow64Arch != null && wow64Arch.endsWith("64") ? "64" : "32";
String setWin = ((realArch.contains("64")) ? "SOFTWARE\\Wow6432Node\\path" : "SOFTWARE\\path");
String check = Advapi32Util.registryGetStringValue (HKEY_LOCAL_MACHINE, setWin, "InstallDir");

哪个字符串指向已安装应用程序的路径,但如果注册表不存在,我的程序将停止。 我如何检查注册表是否存在以及是否不绕过它?

如果密钥不存在,还可以添加system.err.println("Application not installed.");

PS:程序是一个GUI,所以我想显示它,即使密钥丢失了。

谢谢!

1 个答案:

答案 0 :(得分:0)

谢谢山姆。我确实发现了一些东西,不确定它是否会在某一时刻失败......

public void checkInstalled(){
    try {
        String regValue = null;
            regValue = WinRegistry.valueForKey(WinRegistry.HKEY_LOCAL_MACHINE, setWin, "InstallDir");
        if(regValue == null){
            System.err.println("Application not installed!");
        } else {
            "do the other thing"
            }
        } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException | IOException ex) {
        System.err.println(ex);
        }
    }
相关问题