简体   繁体   English

如何使用Java编写Windows注册表?

[英]How to write windows registry using Java?

I am not very well versed in using the Preference Class. 我对使用Preference Class不太了解。 However from reading on here and other websites I feel that what I have should be enough to write into a node of my choosing. 但是,通过在这里和其他网站上阅读,我觉得我所拥有的应该足以写入我选择的节点。

Preferences userPref = Preferences.userRoot();
    userPref = userPref.node("HKEY_CURRENT_USER\\Software\\Broadcom");
    userPref.put("A", "B");

When trying to write into the node "HKEY_CURRENT_USER\\Software\\Broadcom" will only writer into the JavaSoft node. 尝试写入节点“ HKEY_CURRENT_USER \\ Software \\ Broadcom”时,只会写入JavaSoft节点。 Is there anyway to change the subkey to allow me to write into any node I'd like? 无论如何,有没有要更改子项以允许我写入我想要的任何节点的方法?

PS I have tried writing the access location multiple times in different ways. PS我曾尝试以不同的方式多次写入访问位置。 With forward slashes instead of back slashes, with slashes in front of "HKEY", etc. 用正斜杠代替反斜杠,在“ HKEY”前面用斜杠,等等。

Anyone have any ideas? 有人有想法么?

Download jna libraries from here . 这里下载jna库。 Add jna-4.1.0.jar and jna-platform-4.1.0.jar files to your application classpath. 将jna-4.1.0.jar和jna-platform-4.1.0.jar文件添加到您的应用程序类路径。 And use Advapi32Util utility class to read, write or delete the registery keys. 并使用Advapi32Util实用程序类读取,写入或删除注册密钥。 I test some of the solutions but most of them dont work. 我测试了一些解决方案,但大多数都不起作用。 but jna version works pretty well and has lots of features. 但是jna版本的效果很好,并且具有很多功能。

Here is my test code to update Chromium lastrun value. 这是我的测试代码,用于更新Chromium lastrun值。

import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;

public class Main {

    public static void main(String[] args) throws Exception {
        Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Chromium", "lastrun", "13031598735788802");
    }
}

The Preferences API does not necessarily provide access to the registry at all. 首选项API不一定完全提供对注册表的访问。 Some implementations may use the registry, but you can't count on that. 某些实现可能使用注册表,但是您不能指望它。 And it's likely that if an implementation does use the registry, the root of the Preferences hierarchy will be under "HKEY_CURRENT_USER\\Software\\Java" or something like that, and you won't be able to access anything outside that tree. 而且,如果实现确实使用注册表,则“首选项”层次结构的根目录可能位于“ HKEY_CURRENT_USER \\ Software \\ Java”或类似的目录下,并且您将无法访问该树之外的任何内容。

In general, if your question takes the form "How do I use [platform specific feature] in Java?", the answer is either "use JNI" or "don't use Java." 通常,如果您的问题采用“如何在Java中使用[特定于平台的功能]?”的形式,则答案为“使用JNI”或“不使用Java”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM