[英]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我找到了关于这个问题的第一个回复: Read/write to Windows registry using Java
I have copied the class from the reply and used it.我从回复中复制了class并使用了它。
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.我得到 null。
screen from my registry editor of the component I have to read从我必须阅读的组件的注册表编辑器屏幕
I have to read the date value of EnableSecuritySignature.我必须读取 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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.