读取注册表比较值到 if 语句



我有一个读取注册表的工作代码。我的问题是我阅读注册表的部分,我想比较一些 if 语句以最终根据答案做一些事情。我检查了该值,它输出Windows 7专业版作为值,我的if语句不同意。

我的注册表代码来自这里

public class Regtest {
    public static void main(String[] args) { 
    String value = null;
    String os7 = "Windows 7 Professional";
    try {
        value = Registry.readString ( 
                Registry.HKEY_LOCAL_MACHINE,                             //HKEY 
               "SOFTWARE\Microsoft\Windows NT\CurrentVersion",           //Key 
               "ProductName");
        if(value == os7 ){
            System.out.println("Windows Distribution = " + value);
        }else{
            if(value != os7 ){
                System.out.println("Windows Distribution = Wrong OS");  
            }
        }
    } catch (IllegalArgumentException e) {
                e.printStackTrace();
    } catch (IllegalAccessException e) {
                e.printStackTrace();
    } catch (InvocationTargetException e) {
                e.printStackTrace();
            }                                              
        }  
    }

你需要使用 value.equals(os7)。Java 不支持字符串的 ==。它只检查不是给定的对象标识。

最新更新