免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1847 | 回复: 1
打印 上一主题 下一主题

Java获取系统软件安装列表 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-08 19:51 |只看该作者 |倒序浏览
Java获取系统软件安装列表













Java代码
  1. package com.kevin.demo;   
  2. /**  
  3. * @author  <a href="mailto:foohsinglong@gmail.com">kevin.long</a>  
  4. * @description Java获取系统软件安装列表,代码核心来自网上,主要通过Runtime实现,  
  5. *  用JNI也行,解决乱码问题  
  6. */  
  7. import java.io.BufferedReader;   
  8. import java.io.IOException;   
  9. import java.io.InputStreamReader;   
  10. import java.nio.charset.Charset;   
  11.   
  12. import javax.swing.JFrame;   
  13. import javax.swing.JScrollPane;   
  14. import javax.swing.JTable;   
  15. import javax.swing.JTextPane;   
  16.    
  17. public class SystemSoftware {     
  18.     private JFrame f = new JFrame("本系统已经安装的软件列表");   
  19.     private JTextPane textPane = new JTextPane();   
  20.     private MyTable myTable=new MyTable();   
  21.     public static Charset charset = Charset.forName("GBK");   
  22.     public SystemSoftware() {   
  23.         f.setLocation(300, 200);   
  24.         f.setSize(800,500);   
  25.         JScrollPane jScrollPane = new JScrollPane(myTable.getTable());   
  26.         f.add(jScrollPane);   
  27.         f.setVisible(true);   
  28.         f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);   
  29.   
  30.         try {   
  31.             check();   
  32.         } catch (Exception e) {   
  33.             e.printStackTrace();   
  34.         }   
  35.     }   
  36.   
  37.     private void check() throws Exception {   
  38.         textPane.setText("您已经安装的软件:");   
  39.         Runtime runtime = Runtime.getRuntime();   
  40.         Process process = null;   
  41.         process = runtime   
  42.                 .exec("cmd /c reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");   
  43.         BufferedReader in = new BufferedReader(new InputStreamReader(process   
  44.                 .getInputStream(),"GBK"));   
  45.         String string = null;   
  46.         while ((string = in.readLine()) != null) {   
  47.             process = runtime.exec("cmd /c reg query " + string   
  48.                     + " /v DisplayName");   
  49.             BufferedReader name = new BufferedReader(new InputStreamReader(   
  50.                     process.getInputStream(),"GBK"));   
  51.             String[] message = queryValue(string);   
  52.             if(message!=null) myTable.addRow(message);   
  53.             f.repaint();   
  54.         }   
  55.         in.close();   
  56.         process.destroy();   
  57.   
  58.     }   
  59.   
  60.     //具体查询每一个软件的详细信息   
  61.     private String[] queryValue(String string) throws IOException {   
  62.         String nameString = "";   
  63.         String versionString = "";   
  64.            
  65.         String publisherString="";   
  66.         String uninstallPathString = "";   
  67.            
  68.         Runtime runtime = Runtime.getRuntime();   
  69.         Process process = null;   
  70.         BufferedReader br = null;   
  71.            
  72.         process = runtime.exec("cmd /c reg query " + string + " /v DisplayName");   
  73.         br = new BufferedReader(new InputStreamReader(process   
  74.                 .getInputStream(),"GBK"));   
  75.         br.readLine();br.readLine();//去掉前两行无用信息   
  76.         if((nameString=br.readLine())!=null){   
  77.             nameString=nameString.replaceAll("DisplayName    REG_SZ    ", "");  //去掉无用信息   
  78.         }   
  79.            
  80.   
  81.         process = runtime.exec("cmd /c reg query " + string + " /v DisplayVersion");   
  82.         br = new BufferedReader(new InputStreamReader(process   
  83.                 .getInputStream(),"GBK"));   
  84.         br.readLine();br.readLine();//去掉前两行无用信息   
  85.         if((versionString=br.readLine())!=null){   
  86.             versionString=versionString.replaceAll("DisplayVersion    REG_SZ    ", ""); //去掉无用信息   
  87.         }   
  88.            
  89.         process = runtime.exec("cmd /c reg query " + string + " /v Publisher");   
  90.         br = new BufferedReader(new InputStreamReader(process   
  91.                 .getInputStream(),"GBK"));   
  92.         br.readLine();br.readLine();//去掉前两行无用信息   
  93.         if((publisherString=br.readLine())!=null){   
  94.             publisherString =publisherString.replaceAll("Publisher    REG_SZ    ", ""); //去掉无用信息   
  95.         }   
  96.            
  97.         process = runtime.exec("cmd /c reg query " + string + " /v UninstallString");   
  98.         br = new BufferedReader(new InputStreamReader(process   
  99.                 .getInputStream(),"GBK"));   
  100.         br.readLine();br.readLine();//去掉前两行无用信息   
  101.         if((uninstallPathString=br.readLine())!=null){   
  102.             uninstallPathString=uninstallPathString.replaceAll("UninstallString    REG_SZ    ", "");    //去掉无用信息   
  103.         }   
  104.            
  105.         String[] resultString=new String[4];   
  106.         resultString[0]= nameString ;//== null ? null : new String(nameString.getBytes(),"GB-2312");   
  107.         resultString[1]= versionString ;//== null ? null : new String(versionString.getBytes(),"GB-2312");   
  108.         resultString[2]= publisherString ;//== null ? null : new String(publisherString.getBytes(),"GB-2312");   
  109.         resultString[3]= uninstallPathString ;//== null ? null : new String(uninstallPathString.getBytes(),"GB-2312");   
  110.         if(resultString[0]==null) resultString=null;    //没有名字的不显示   
  111.         return resultString;   
  112.     }   
  113.       
  114.     //列表   
  115.     private class MyTable{   
  116.         private JTable jTable;   
  117.         private Object[][] data=new Object[100][4];   
  118.         private Object[] colNames= { "软件名称","版本号","出版商","卸载路径"};   
  119.         private int p=-1;   
  120.            
  121.         public MyTable(){   
  122.                
  123.         }   
  124.            
  125.         public void addRow(Object[] data){   
  126.             p++;   
  127.             if(p>=100) return ;   
  128.             this.data[p]=data;   
  129.         }   
  130.            
  131.            
  132.         public JTable getTable(){   
  133.             jTable=new JTable(data,colNames);   
  134.             return jTable;   
  135.         }   
  136.            
  137.     }   
  138.       
  139.     public static void main(String[] args) {   
  140.         new SystemSoftware();   
  141.     }   
  142. }   
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-23 20:10 |只看该作者
谢谢分享   学习鸟
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP