简体   繁体   English

如何在Windows注册表中添加新变量

[英]How to add a new variable in the Windows-Registry

I need to create a new variable in the Registry key and put a value into it, using com.ice.jni.registry . 我需要在注册表项中创建一个新变量,并使用com.ice.jni.registry将值放入其中。 Examples which I have found on the internet don't explain how to do it. 我在互联网上找到的示例并未说明如何执行此操作。 I know how to get the existing value, and set a new value in the existing key.\\ 我知道如何获取现有值,并在现有密钥中设置新值。\\

Regestry API Regestry API

I Find some code wich shoud edit the value if it is already created. 我发现一些代码,如果已创建该值,则应对其进行编辑。 Obvious i am getting "NullPointerException" 显而易见,我收到“ NullPointerException”

try {
    Registry registry = new Registry();
    registry.debugLevel=true;
    RegistryKey regkey = Registry.HKEY_CURRENT_USER;
    RegistryKey key =registry.openSubkey(regkey,"Software\\Microsoft\\4Game\\AceOnline",RegistryKey.ACCESS_ALL);

    RegStringValue stringValue = new RegStringValue(key,"javaci",RegistryValue.REG_SZ);
    stringValue.setData("Hello"); 
    key.setValue(stringValue); //NullPointer Exception here

    }
    catch(RegistryException ex) {
        ex.printStackTrace();
    }  

It can be a real pain to get anything done with JNI because: 1. The DLL must be on the path. 使用JNI完成任何操作可能会非常痛苦,因为:1. DLL必须在路径上。 2. You can't copy a DLL to system32 because of the added security in Vista and Windows 7 3. The DLL must be available in both 32 and 64 bit versions in case the JVM being used is 32 or 64 bit. 2.由于Vista和Windows 7增加了安全性,因此无法将DLL复制到system32。3.如果使用的JVM是32位或64位,则DLL必须同时具有32位和64位版本。

You can avoid a lot of problems by just calling the reg command line tool built into Windows. 您只需调用Windows内置的reg命令行工具,就可以避免很多问题。

In a command prompt type reg add /? 在命令提示符下键入reg add /?

You can call a command like this using Runtime.getRuntime().exec(). 您可以使用Runtime.getRuntime()。exec()调用这样的命令。

You're in luck. 你真幸运。 I have some code that uses this from Java. 我有一些使用Java中的代码的代码。 It's not very pretty though so good luck; 虽然运气很好,但不是很漂亮。 I make no guarantees that it works and won't destroy your registry. 我不保证它会起作用,也不会破坏您的注册表。

public class WinUtil {

    //returns mappings as (extension, mimetype) (without periods)
    public static Map<String, String> getMimeMappings() throws Exception {
        Map<String, String> extensions = new HashMap<String, String>();
        String result = WinUtil.doRegQuery("HKEY_CLASSES_ROOT\\Mime\\Database\\Content Type", "/s");
        if (result == null) {
            return null;
        }

        String path = null;
        String[] lines = result.split("\n");
        for (String line : lines) {

            line = line.replaceAll("\r", "");
            if (line.startsWith(" ") || line.startsWith(" ")) {
                line = line.trim();
                if (line.startsWith("Extension")) {
                    String[] entries = line.split("    ");
                    if (entries.length >= 3) {
                        String ext = entries[2].substring(1); //skip .                       

                        // we only split here, since Extension may not have been found, in which case it would have been a waste
                        String[] pathSplit = path.split("\\\\");
                        String key = pathSplit[pathSplit.length - 1];

                        extensions.put(ext.toLowerCase(), key.toLowerCase());
                    }
                }

            } else {
                line = line.trim();
                path = line;
            }
        }
        return extensions;
    }

    //return key locations and keys 
    public static String[] getRegSubKeys(String regLocation) {

        try {
            String result = doRegQuery(regLocation, "");

            if (result == null) {
                return null;
            } else if (result.isEmpty()) {
                return new String[0];      // empty string array
            }
            String[] strArray = result.split("\n");
            //remove form-feed characters, if any
            for (int i = 0; i < strArray.length; i++) {
                strArray[i] = strArray[i].replaceAll("\r", "");
            }
            return strArray;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //note when using this to use the abbreviated forms: i.e. 
    // HKCU, HKLM, etc.
    public static String setRegKey(String location, String regKey, String regValue) throws Exception {
        String regCommand = "add \"" + location + "\" /v \"" + regKey + "\" /f /d \"" + regValue + "\"";

        return doReg(regCommand);
    }

    public static String getRegKey(String regKey, String regKeyType, String regLocation) throws Exception {

        String regCommand = "/v \"" + regKey + "\"";

        String result = doRegQuery(regLocation, regCommand);

        int p = result.indexOf(regKeyType);
        if (p == -1) {
            throw new Exception("Could not find type of key in REG utility output");
        }
        return result.substring(p + regKeyType.length()).trim();
    }

    //may return null if exec was unsuccessful
    public static String doRegQuery(String regLocation, String regCommand) throws Exception {
        final String REGQUERY_UTIL = "Reg Query" + " ";
        final String regUtilCmd = REGQUERY_UTIL + "\"" + regLocation + "\" " + regCommand;
        return runProcess(regUtilCmd);
    }

    public static String doReg(String regCommand) throws Exception {

        final String REG_UTIL = "reg";
        final String regUtilCmd = REG_UTIL + " " + regCommand;
        return runProcess(regUtilCmd);
    }

    public static String runProcess(final String regUtilCmd) throws Exception {
        StringWriter sw = new StringWriter();
        Process process = Runtime.getRuntime().exec(regUtilCmd);

        InputStream is = process.getInputStream();
        int c = 0;
        while ((c = is.read()) != -1) {
            sw.write(c);
        }
        String result = sw.toString();
        try {
            process.waitFor();
        } catch (Throwable ex) {
            System.out.println(ex.getMessage());
        }
        if (process.exitValue() == -1) {
            throw new Exception ("REG QUERY command returned with exit code -1");
        }
        return result;
    }
}

如果可以掌握要创建的Key的父级,则可以尝试使用RegistryKey.createSubKey(java.lang.String subkey,java.lang.String className)

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

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