package reg;
import java.io.BufferedInputStream;
import java.io.IOException;
public class V3 {
private static final String CMD_REG_QUERY="reg query";
private static final String TOKEN_REGSTR="REG_SZ";
private static final String TOKEN_REGDWORD="REG_DWORD";
public static String getRegistryValue(String node, String key){
BufferedInputStream in = null;
String regData=null;
//cmd 창에서 아래 거 복사,붙여넣기 해도 나온다.
//reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Ahnlab\ASPack\9.0" /v AscVersion
try{
String strCmd=CMD_REG_QUERY+" \""+node+"\" /v " + key;
System.out.println(strCmd);
Process process=Runtime.getRuntime().exec(strCmd);
in = new BufferedInputStream(process.getInputStream());
try{
Thread.sleep(200);
}catch(InterruptedException ie){
ie.printStackTrace();
}
byte[] buff=new byte[in.available()];
in.read(buff);
regData=new String(buff);
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
try{in.close();}catch(IOException ioe){}
}
int index=regData.indexOf(TOKEN_REGSTR);
if(index<0)
return null;
return regData.substring(index+TOKEN_REGSTR.length()).trim();
}
public static void main(String[] args) {
String node="HKEY_LOCAL_MACHINE\\SOFTWARE\\Ahnlab\\ASPack\\9.0";
String key="AscVersion";
System.out.println(getRegistryValue(node,key));
}
}
/*
결과:
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Ahnlab\ASPack\9.0" /v AscVersion
2021.09.27.00
*/