使用 java.util.prefs.Preferences 处理 Windows 注册表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2548307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Working with Windows registry using java.util.prefs.Preferences
提问by Stan Kurilin
I have some questions about the registry.
We have
我有一些关于注册表的问题。
我们有
Preferences p = Preferences.userRoot();
If we execute
如果我们执行
p.nodeExists("/HKEY_CURRENT_USER/Software/Policies/Microsoft")
it will return true.
After it:
它会返回真。
之后:
p = p.node("/HKEY_CURRENT_USER/Software/Policies");
for(String s : p.childrenNames()){
System.out.println(">" + s);
}
We see that it has one child: "Windows". But
我们看到它有一个孩子:“Windows”。但
p.nodeExists("/HKEY_CURRENT_USER/Software/Policies/Microsoft/Windows")
returns false. Why?
返回假。为什么?
Thanks.
谢谢。
UPDATE
更新
Ok. I have some mistakes. Let me try again: Why does
好的。我有一些错误。让我再试一次:为什么
p.nodeExists("/HKEY_CURRENT_USER/Software/Policies/Microsoft/Windows")
return false?
返回假?
回答by Péter T?r?k
If you execute the code lines shown, in the order shown, when you get to the line
如果您按显示的顺序执行显示的代码行,当您到达该行时
p.nodeExists("/HKEY_CURRENT_USER/Software/Policies/Microsoft/Windows")
p
does not anymore point to the user root, but to "/HKEY_CURRENT_USER/Software/Policies".
p
不再指向用户 root,而是指向“/HKEY_CURRENT_USER/Software/Policies”。
Btw you have a probable omission in your third code sample:
顺便说一句,您的第三个代码示例中可能有遗漏:
p = p.node("/HKEY_CURRENT_USER/Software/Policies");
should be
应该
p = p.node("/HKEY_CURRENT_USER/Software/Policies/Microsoft");
回答by peterh
I stumbled on this one today. The answer you've accepted is completely wrong.
我今天偶然发现了这个。你接受的答案是完全错误的。
You seem to be under the impression that Java Preferencesis a general tool to manipulate the Windows Registry. It is not. It just so happens that the default implementation of Preferences on the Windows platform happens to store its data in the Windows Registry.
您似乎认为Java 首选项是操作 Windows 注册表的通用工具。它不是。碰巧的是,Windows 平台上首选项的默认实现恰好将其数据存储在 Windows 注册表中。
The implementation on Windows stores stuff under the following Registry paths:
Windows 上的实现将内容存储在以下注册表路径下:
For systemRoot: HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs
对于系统根: HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs
For userRoot : HKEY_CURRENT_USER\Software\JavaSoft\Prefs
对于 userRoot : HKEY_CURRENT_USER\Software\JavaSoft\Prefs
(note: The registry paths changes a bit if you are using a 32bit JRE on a 64-bit OS but that has nothing to do with Java and everything to do with Windows. Sun's code always use the above paths.)
(注意:如果您在 64 位操作系统上使用 32 位 JRE,则注册表路径会发生一些变化,但这与 Java 无关,而与 Windows 无关。Sun 的代码始终使用上述路径。)
The point to make is that you can maybe use Java Preferences interface to read or change values in the Windows Registry but only belowthe above registry paths.The reason why I say 'maybe' is that this is just how it happens to be at the moment. Sun/Oracle could at any point in time decide to not to use the Windows Registry or use the Windows Registry but without using sub-nodes, i.e. store everything in one big XML string or something. The point is that Java Preferences is designed to shield you from this.
重点是您可以使用 Java Preferences 接口来读取或更改 Windows 注册表中的值,但只能在上述注册表路径下方。我之所以说“也许”,是因为这正是目前的情况。Sun/Oracle 可以随时决定不使用 Windows 注册表或使用 Windows 注册表但不使用子节点,即将所有内容存储在一个大的 XML 字符串或其他东西中。关键是 Java 首选项旨在保护您免受此影响。
A lot of Java software that use the Java Preferences provide their own implementation (which is pretty simple to do) to avoid Sun's default implementation that uses the Windows Registry. Not everyone can write to the Windows Registry these days so that was a pretty bad design decision on Sun's part. Fortunately very easy to change.
许多使用 Java 首选项的 Java 软件都提供了自己的实现(这很简单),以避免 Sun 使用 Windows 注册表的默认实现。现在并不是每个人都可以写入 Windows 注册表,因此对于 Sun 来说这是一个非常糟糕的设计决定。好在很容易改变。