使用Java读/写Windows注册表
如何使用Java读取/写入Windows注册表?
解决方案
回答
是的,使用java.util.Preferences API,因为它的Windows实现使用注册表作为后端。
最后,这取决于我们要执行的操作:存储应用程序的首选项才是Preferences最好的选择。如果我们实际上想更改与应用程序无关的注册表项,则需要一些JNI应用程序,如Mark所描述的(这里是无耻的窃取):
From a quick google: Check the WinPack for JNIWrapper. It has full Windows Registry access support including Reading and Writing. The WinPack Demo has Registry Viewer implemented as an example. Check at http://www.teamdev.com/jniwrapper/winpack/#registry_access And... There is also try JNIRegistry @ http://www.trustice.com/java/jnireg/ There is also the option of invoking an external app, which is responsible for reading / writing the registry.
回答
从快速的Google:
Check the WinPack for JNIWrapper. It has full Windows Registry access support including Reading and Writing. The WinPack Demo has Registry Viewer implemented as an example. Check at http://www.teamdev.com/jniwrapper/winpack/#registry_access
和...
There is also try JNIRegistry @ http://www.trustice.com/java/jnireg/
还可以选择调用外部应用程序,该应用程序负责读取/写入注册表。
回答
首选项API方法不能让我们访问注册表的所有分支。实际上,它仅使我们可以访问Preferences API存储其首选项的位置。它不是通用的注册表处理API,例如.NET的
正如Mark所示,我想采用JNI或者外部工具来读取/写入每个密钥。
回答
很少有JNDI服务提供商可以使用Windows注册表。
可以观察http://java.sun.com/products/jndi/serviceproviders.html。
回答
在使用jRegistryKey之前,我已经完成了此操作。这是一个LGPL Java / JNI库,可以满足需求。这是一个示例,说明了如何使用它通过regedit启用注册表编辑以及通过注册表在Windows中为自己启用"显示文件夹选项"选项。
import java.io.File; import ca.beq.util.win32.registry.RegistryKey; import ca.beq.util.win32.registry.RegistryValue; import ca.beq.util.win32.registry.RootKey; import ca.beq.util.win32.registry.ValueType; public class FixStuff { private static final String REGEDIT_KEY = "Software\Microsoft\Windows\CurrentVersion\Policies\System"; private static final String REGEDIT_VALUE = "DisableRegistryTools"; private static final String REGISTRY_LIBRARY_PATH = "\lib\jRegistryKey.dll"; private static final String FOLDER_OPTIONS_KEY = "Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"; private static final String FOLDER_OPTIONS_VALUE = "NoFolderOptions"; public static void main(String[] args) { //Load JNI library RegistryKey.initialize( new File(".").getAbsolutePath()+REGISTRY_LIBRARY_PATH ); enableRegistryEditing(true); enableShowFolderOptions(true); } private static void enableShowFolderOptions(boolean enable) { RegistryKey key = new RegistryKey(RootKey.HKEY_CURRENT_USER,FOLDER_OPTIONS_KEY); RegistryKey key2 = new RegistryKey(RootKey.HKEY_LOCAL_MACHINE,FOLDER_OPTIONS_KEY); RegistryValue value = new RegistryValue(); value.setName(FOLDER_OPTIONS_VALUE); value.setType(ValueType.REG_DWORD_LITTLE_ENDIAN); value.setData(enable?0:1); if(key.hasValue(FOLDER_OPTIONS_VALUE)) { key.setValue(value); } if(key2.hasValue(FOLDER_OPTIONS_VALUE)) { key2.setValue(value); } } private static void enableRegistryEditing(boolean enable) { RegistryKey key = new RegistryKey(RootKey.HKEY_CURRENT_USER,REGEDIT_KEY); RegistryValue value = new RegistryValue(); value.setName(REGEDIT_VALUE); value.setType(ValueType.REG_DWORD_LITTLE_ENDIAN); value.setData(enable?0:1); if(key.hasValue(REGEDIT_VALUE)) { key.setValue(value); } } }
回答
The WinPack Demo has Registry Viewer implemented as an example. Check at http://www.jniwrapper.com/winpack_features.jsp#registry
顺便说一句,WinPack已移至以下地址:
http://www.teamdev.com/jniwrapper/winpack/
回答
我们可以尝试WinRun4J。这是Windows Java启动器和服务主机,但它也提供用于访问注册表的库。
(顺便说一下,我正在从事这个项目,所以如果我们有任何疑问,请告诉我)
回答
我们实际上不需要第三者套餐。 Windows具有用于所有注册表操作的reg实用程序。要获取命令格式,请转到DOS属性并键入:
reg /?
我们可以通过Runtime类调用reg:
Runtime.getRuntime().exec("reg <your parameters here>");
使用上面的命令,编辑密钥和添加新密钥非常简单。要读取注册表,我们需要获取reg的输出,这有些棘手。这是代码:
import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; /** * @author Oleg Ryaboy, based on work by Miguel Enriquez */ public class WindowsReqistry { /** * * @param location path in the registry * @param key registry key * @return registry value or null if not found */ public static final String readRegistry(String location, String key){ try { // Run reg query, then read output with StreamReader (internal class) Process process = Runtime.getRuntime().exec("reg query " + '"'+ location + "\" /v " + key); StreamReader reader = new StreamReader(process.getInputStream()); reader.start(); process.waitFor(); reader.join(); String output = reader.getResult(); // Output has the following format: // \n<Version information>\n\n<key>\t<registry type>\t<value> if( ! output.contains("\t")){ return null; } // Parse out the value String[] parsed = output.split("\t"); return parsed[parsed.length-1]; } catch (Exception e) { return null; } } static class StreamReader extends Thread { private InputStream is; private StringWriter sw= new StringWriter(); public StreamReader(InputStream is) { this.is = is; } public void run() { try { int c; while ((c = is.read()) != -1) sw.write(c); } catch (IOException e) { } } public String getResult() { return sw.toString(); } } public static void main(String[] args) { // Sample usage String value = WindowsReqistry.readRegistry("HKCU\Software\Microsoft\Windows\CurrentVersion\" + "Explorer\Shell Folders", "Personal"); System.out.println(value); } }