-1

我有一个用于读取注册表的工作代码。我的问题是我读取注册表的部分,我想比较一些 if 语句以最终根据答案做一些事情。我检查了值,它输出 Windows 7 Professional 作为值,我的 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();
            }                                              
        }  
    }
4

1 回答 1

0

You need to use value.equals(os7). Java does not support == for Strings. It only checks for object identity which is not a given.

于 2012-08-08T18:58:24.470 回答