簡體   English   中英

如何在Windows注冊表中添加新變量

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

我需要在注冊表項中創建一個新變量,並使用com.ice.jni.registry將值放入其中。 我在互聯網上找到的示例並未說明如何執行此操作。 我知道如何獲取現有值,並在現有密鑰中設置新值。\\

Regestry API

我發現一些代碼,如果已創建該值,則應對其進行編輯。 顯而易見,我收到“ 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();
    }  

使用JNI完成任何操作可能會非常痛苦,因為:1. DLL必須在路徑上。 2.由於Vista和Windows 7增加了安全性,因此無法將DLL復制到system32。3.如果使用的JVM是32位或64位,則DLL必須同時具有32位和64位版本。

您只需調用Windows內置的reg命令行工具,就可以避免很多問題。

在命令提示符下鍵入reg add /?

您可以使用Runtime.getRuntime()。exec()調用這樣的命令。

你真幸運。 我有一些使用Java中的代碼的代碼。 雖然運氣很好,但不是很漂亮。 我不保證它會起作用,也不會破壞您的注冊表。

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