简体   繁体   中英

How to read integer value from Windows registry using Java?

I have found the first reply on this question: Read/write to Windows registry using Java

I have copied the class from the reply and used it.

This sample code works:

String value = WinRegistry.readString (
    WinRegistry.HKEY_LOCAL_MACHINE,                             //HKEY
   "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",           //Key
   "ProductName");                                              //ValueName
    System.out.println("Windows Distribution = " + value);

However, when I try to run:

String value = WinRegistry.readString(
                WinRegistry.HKEY_LOCAL_MACHINE,                             //HKEY
                "SYSTEM\\CurrentControlSet\\Services\\LanmanWorkstation\\Parameters",           //Key
                "EnableSecuritySignature");                                              //ValueName
        System.out.println(value);

I get null.

screen from my registry editor of the component I have to read

I have to read the date value of EnableSecuritySignature.

Does someone know what could be the problem? Sorry for lack of details in my description, I am in hurry))

I have found the solution:

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

public class ReadRegistry
{
    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();

            // Parse out the value
            // String[] parsed = reader.getResult().split("\\s+");

            String s1[];
            try{
             s1=reader.getResult().split("REG_SZ|REG_DWORD");
            }
            catch(Exception e)
            {
                return " ";
            }
           //MK System.out.println(s1[1].trim());

            return s1[1].trim();
        } 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(c);
                    sw.write(c);
            } catch (IOException e) {
            }
        }

        public String getResult() {
            return sw.toString();
        }
    }
}

You have to use it like this:

Registry2.readRegistry("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\LanmanWorkstation\\Parameters", "EnableSecuritySignature");

The class was taken from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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