Java

The simplest method I have found to read REG_DWORD(Dword) and REG_SZ(String) registry both by using the following code

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();
}
}
}

How to use this class

code is below

String outputValue = ReadRegistry.readRegistry(REGISTRY_PATH, REGISTRY_KEY);

Note: REGISTRY_PATH and REGISTRY_KEY are constants. Its value depends upon your query path and query key.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s