/***@authorkevin.long
* @description Java获取系统软件安装列表,代码核心来自 上,主要通过Runtime实现,
* 用JNI也行,解决乱码问题*/
importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.nio.charset.Charset;importjavax.swing.JFrame;importjavax.swing.JScrollPane;importjavax.swing.JTable;importjavax.swing.JTextPane;public classSystemSoftware {private JFrame f = new JFrame(“本系统已经安装的软件列表”);private JTextPane textPane = newJTextPane();private MyTable myTable=newMyTable();public static Charset charset = Charset.forName(“GBK”);publicSystemSoftware() {
f.setLocation(300, 200);
f.setSize(800,500);
JScrollPane jScrollPane= newJScrollPane(myTable.getTable());
f.add(jScrollPane);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);try{
check();
}catch(Exception e) {
e.printStackTrace();
}
}private void check() throwsException {
textPane.setText(“您已经安装的软件:”);
Runtime runtime=Runtime.getRuntime();
Process process= null;
process=runtime
.exec(“cmd /c reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\”);
BufferedReader in= new BufferedReader(newInputStreamReader(process
.getInputStream(),”GBK”));
String string= null;while ((string = in.readLine()) != null) {
process= runtime.exec(“cmd /c reg query ” +string+ ” /v DisplayName”);
BufferedReader name= new BufferedReader(newInputStreamReader(
process.getInputStream(),”GBK”));
String[] message=queryValue(string);if(message!=null) myTable.addRow(message);
f.repaint();
}
in.close();
process.destroy();
}//具体查询每一个软件的详细信息
private String[] queryValue(String string) throwsIOException {
String nameString= “”;
String versionString= “”;
String publisherString=””;
String uninstallPathString= “”;
Runtime runtime=Runtime.getRuntime();
Process process= null;
BufferedReader br= null;
process= runtime.exec(“cmd /c reg query ” + string + ” /v DisplayName”);
br= new BufferedReader(newInputStreamReader(process
.getInputStream(),”GBK”));
br.readLine();br.readLine();//去掉前两行无用信息
if((nameString=br.readLine())!=null){
nameString=nameString.replaceAll(“DisplayName REG_SZ “, “”); //去掉无用信息
}
process= runtime.exec(“cmd /c reg query ” + string + ” /v DisplayVersion”);
br= new BufferedReader(newInputStreamReader(process
.getInputStream(),”GBK”));
br.readLine();br.readLine();//去掉前两行无用信息
if((versionString=br.readLine())!=null){
versionString=versionString.replaceAll(“DisplayVersion REG_SZ “, “”); //去掉无用信息
}
process= runtime.exec(“cmd /c reg query ” + string + ” /v Publisher”);
br= new BufferedReader(newInputStreamReader(process
.getInputStream(),”GBK”));
br.readLine();br.readLine();//去掉前两行无用信息
if((publisherString=br.readLine())!=null){
publisherString=publisherString.replaceAll(“Publisher REG_SZ “, “”); //去掉无用信息
}
process= runtime.exec(“cmd /c reg query ” + string + ” /v UninstallString”);
br= new BufferedReader(newInputStreamReader(process
.getInputStream(),”GBK”));
br.readLine();br.readLine();//去掉前两行无用信息
if((uninstallPathString=br.readLine())!=null){
uninstallPathString=uninstallPathString.replaceAll(“UninstallString REG_SZ “, “”); //去掉无用信息
}
String[] resultString=new String[4];
resultString[0]= nameString ;//== null ull : new String(nameString.getBytes(),”GB-2312″);
resultString[1]= versionString ;//== null ull : new String(versionString.getBytes(),”GB-2312″);
resultString[2]= publisherString ;//== null ull : new String(publisherString.getBytes(),”GB-2312″);
resultString[3]= uninstallPathString ;//== null ull : new String(uninstallPathString.getBytes(),”GB-2312″);
if(resultString[0]==null) resultString=null; //没有名字的不显示
returnresultString;
}//列表
private classMyTable{privateJTable jTable;private Object[][] data=new Object[100][4];private Object[] colNames= { “软件名称”,”版本 ”,”出版商”,”卸载路径”};private int p=-1;publicMyTable(){
}public voidaddRow(Object[] data){
p++;if(p>=100) return;this.data[p]=data;
}publicJTable getTable(){
jTable=newJTable(data,colNames);returnjTable;
}
}public static voidmain(String[] args) {newSystemSoftware();
}
}
文章知识点与官方知识档案匹配,可进一步学习相关知识Java技能树首页概览93913 人正在系统学习中 相关资源:进玉电极模块_v6.1_nx7.0到8.5_(64位UG)_简体_正式版.rar-制造…
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!