Accessing Windows Registry [duplicate]

不羁的心 提交于 2019-12-21 11:13:29

问题


Possible Duplicate:
read/write to Windows Registry using Java

I need to access Windows registry from Java.. Also I need to copy some registry entries and may have to enter new registry variables using Java.. some one help me please...


回答1:


I'd recommend the Java Native Access (JNA) library. It's a pretty nice wrapper around JNI. According to this mailing list post, they've already got a contributed wrapper around the native Windows registry function calls.

If you add the JNA libraries to your project, the relevant source you'll want is the Registry.java class. From there, just call methods on that class to investigate the Windows registry.

As a side note, make sure when you use JNA that you use Platform.isXxx() to make sure your code can actually query the registry on the particular platform.




回答2:


An example will be like this:

import com.ice.jni.registry.*;

public class DeleteEnvironmentVar{
public DeleteEnvironmentVar(String variable, String value) throws Exception {

        RegistryKey machine = Registry.getTopLevelKey("HKEY_LOCAL_MACHINE");
        RegistryKey environment = machine.openSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", RegistryKey.ACCESS_WRITE);
        try {
            if ( value == null ) { //Delete the variable in case value is empty
                environment.deleteValue(variable);
            }            
        }
        catch( NoSuchValueException nsve ) {}
        catch( NoSuchKeyException nske ) {}        
    }
}



回答3:


The Preferences class is the Java preferred way of writing to the registry. However, I haven't actually used it, so I don't know if it allows access to the entire registry or just a section specific to the JVM or your application. If it doesn't, then it sounds like for your purpose you'll be needing to look at the JNI solutions posited by others here. If it does work, then you have a platform-independent method of storing off your settings if you ever port it.



来源:https://stackoverflow.com/questions/585804/accessing-windows-registry

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!