首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从注册表编辑器插入和检索注册表项

如何从注册表编辑器插入和检索注册表项
EN

Stack Overflow用户
提问于 2010-12-24 16:06:49
回答 1查看 713关注 0票数 0

我刚接触密码学。我必须开发基于我的项目的cryptography..In部分的项目,我必须插入一个密钥到注册表,然后我必须检索相同的密钥进行解密。直到我找到注册表的路径..

这里我展示了我的代码:

代码语言:javascript
复制
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

public final class Project {

    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) {
                    System.out.println("Reading" + c);
                    sw.write(c);
                }
            } catch (IOException e) {
                System.out.println("Exception in run() " + e);
            }
        }

        public String getResult() {
            System.out.println("Content " + sw.toString());
            return sw.toString();
        }
    }

    public static boolean addValue(String key, String valName, String val) {
        try {
            // Run reg query, then read output with StreamReader (internal class)



            Process process = Runtime.getRuntime().exec("reg add \"" + key + "\" /v \"" + valName + "\" /d \"\\\"" + val + "\\\"\" /f");

            StreamReader reader = new StreamReader(process.getInputStream());
            reader.start();
            process.waitFor();
            reader.join();
            String output = reader.getResult();
            System.out.println("Processing........ggggggggggggggggggggg." + output);
            // Output has the following format:
            // \n&lt;Version information&gt;\n\n&lt;key&gt;\t&lt;registry type&gt;\t&lt;value&gt;
            return output.contains("The operation completed successfully");
        } catch (Exception e) {
            System.out.println("Exception in addValue() " + e);
        }
        return false;
    }

    public static void main(String[] args) {

        // Sample usage
        JAXRDeleteConcept hc = new JAXRDeleteConcept();
        System.out.println("Before Insertion");
        if (JAXRDeleteConcept.addValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU", "REG_SZ", "Muthus")) {
            System.out.println("Inserted Successfully");
        }

        String value = JAXRDeleteConcept.readRegistry("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU" , "Project_Key");
        System.out.println(value);
    }
}

但是我不知道如何在注册表中插入一个注册表项,并读取我inserted..Please帮助我的特定注册表项。提前谢谢..

EN

回答 1

Stack Overflow用户

发布于 2010-12-24 16:51:57

使用JRegistry库编辑注册表要比执行命令容易得多。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4525057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档