2 package org.feistymeow.system;
\r
7 * this class supports reading and setting values in the Windows registry.
\r
9 * some example code came from http://www.rgagnon.com/javadetails/java-0480.html
\r
10 * @author Chris Koeritz
\r
12 public class RegistryEditor {
\r
14 // the values below can be used for the expected type in the registry calls.
\r
15 public static final String STRING_TYPE = "REG_SZ";
\r
16 public static final String DOUBLEWORD_TYPE = "REG_DWORD";
\r
18 // looks up the key provided in the registry and returns true if the key seems to exist.
\r
19 // this is just a check for presence; it could have no subkeys or values under it, but it at
\r
20 // least seems to be there.
\r
21 public static boolean checkKey(String keyName) {
\r
23 // make a command to run in windows that will query the value.
\r
24 StringBuilder command = new StringBuilder(QUERY_COMMAND);
\r
25 command.append("\"");
\r
26 command.append(keyName);
\r
27 command.append("\"");
\r
28 // start the command running and trap its output.
\r
29 Process process = Runtime.getRuntime().exec(command.toString());
\r
30 StreamReader reader = new StreamReader(process.getInputStream());
\r
32 process.waitFor(); // let the system call finish up.
\r
33 reader.join(); // let the stream reader finish also.
\r
34 return process.exitValue() == 0;
\r
35 } catch (Exception e) {
\r
40 // retrieves the key provided from the registry and selects out the value requested.
\r
41 // it is necessary to know the expected type, such as REG_SZ or REG_DWORD.
\r
42 // the value is always returned as a string and needs to be cast to different types as appropriate.
\r
43 public static String getValue(String keyName, String valueName, String typeExpected) {
\r
45 // make a command to run in windows that will query the value.
\r
46 StringBuilder command = new StringBuilder(QUERY_COMMAND);
\r
47 command.append("\"");
\r
48 command.append(keyName);
\r
49 command.append("\"");
\r
50 command.append(VALUE_FLAG);
\r
51 command.append(valueName);
\r
52 // start the command running and trap its output.
\r
53 Process process = Runtime.getRuntime().exec(command.toString());
\r
54 StreamReader reader = new StreamReader(process.getInputStream());
\r
56 process.waitFor(); // let the system call finish up.
\r
57 reader.join(); // let the stream reader finish also.
\r
58 // now grab the results from running the command and extract the answer.
\r
59 String result = reader.getResult();
\r
60 int p = result.indexOf(typeExpected);
\r
61 if (p == -1) return null;
\r
62 // return the result with the stuff before the type chopped off.
\r
63 return result.substring(p + typeExpected.length()).trim();
\r
64 } catch (Exception e) {
\r
69 // makes a change to the specified "keyName" value called "valueName". the old value will be replaced
\r
70 // with the "newValue" provided. the key and the value do not have to exist prior to the call, but if
\r
71 // they already did exist, they'll be updated.
\r
72 public static boolean setValue(String keyName, String valueName, String typeExpected, String newValue) {
\r
74 // make a command to run in windows that will set the value.
\r
75 StringBuilder command = new StringBuilder(SET_COMMAND);
\r
76 command.append("\"");
\r
77 command.append(keyName);
\r
78 command.append("\"");
\r
79 command.append(VALUE_FLAG);
\r
80 command.append(valueName);
\r
81 command.append(TYPE_FLAG);
\r
82 command.append(typeExpected);
\r
83 command.append(FORCE_FLAG);
\r
84 command.append(DATA_FLAG);
\r
85 command.append(newValue);
\r
86 //System.out.println("command to run: " + command);
\r
87 // start the command running and trap its output.
\r
88 Process process = Runtime.getRuntime().exec(command.toString());
\r
89 StreamReader reader = new StreamReader(process.getInputStream());
\r
91 process.waitFor(); // let the system call finish up.
\r
92 reader.join(); // let the stream reader finish also.
\r
93 return (process.exitValue() == 0); // zero exit is a success.
\r
94 } catch (Exception e) {
\r
99 // removes the "valueName" value from the key "keyName". true is returned on success.
\r
100 public static boolean deleteValue(String keyName, String valueName) {
\r
102 // make a command to run in windows that will set the value.
\r
103 StringBuilder command = new StringBuilder(DELETE_COMMAND);
\r
104 command.append("\"");
\r
105 command.append(keyName);
\r
106 command.append("\"");
\r
107 command.append(VALUE_FLAG);
\r
108 command.append(valueName);
\r
109 command.append(FORCE_FLAG);
\r
110 //System.out.println("command to run: " + command);
\r
111 // start the command running and trap its output.
\r
112 Process process = Runtime.getRuntime().exec(command.toString());
\r
113 StreamReader reader = new StreamReader(process.getInputStream());
\r
115 process.waitFor(); // let the system call finish up.
\r
116 reader.join(); // let the stream reader finish also.
\r
117 return (process.exitValue() == 0); // zero exit is a success.
\r
118 } catch (Exception e) {
\r
123 // removes the entire key "keyName" from the registry. true is returned on success.
\r
124 public static boolean deleteKey(String keyName) {
\r
126 // make a command to run in windows that will set the value.
\r
127 StringBuilder command = new StringBuilder(DELETE_COMMAND);
\r
128 command.append("\"");
\r
129 command.append(keyName);
\r
130 command.append("\"");
\r
131 command.append(FORCE_FLAG);
\r
132 //System.out.println("command to run: " + command);
\r
133 // start the command running and trap its output.
\r
134 Process process = Runtime.getRuntime().exec(command.toString());
\r
135 StreamReader reader = new StreamReader(process.getInputStream());
\r
137 process.waitFor(); // let the system call finish up.
\r
138 reader.join(); // let the stream reader finish also.
\r
139 return (process.exitValue() == 0); // zero exit is a success.
\r
140 } catch (Exception e) {
\r
145 // constants used in the registry code for talking to windows' reg application.
\r
146 private static final String QUERY_COMMAND = "reg query ";
\r
147 private static final String VALUE_FLAG = " /v ";
\r
149 private static final String SET_COMMAND = "reg add ";
\r
150 private static final String TYPE_FLAG = " /t ";
\r
151 private static final String DATA_FLAG = " /d ";
\r
152 private static final String FORCE_FLAG = " /f ";
\r
154 private static final String DELETE_COMMAND = "reg delete ";
\r
156 // wrapper class for stream reading came from web example mentioned above.
\r
157 static class StreamReader extends Thread {
\r
158 private InputStream is;
\r
159 private StringWriter sw;
\r
161 StreamReader(InputStream is) {
\r
163 sw = new StringWriter();
\r
166 public void run() {
\r
169 while ((c = is.read()) != -1) sw.write(c);
\r
170 } catch (IOException e) { /*nothing*/ }
\r
173 String getResult() { return sw.toString(); }
\r