使用 Java 访问 Windows 注册表

来源:百度文库 编辑:神马文学网 时间:2024/04/28 22:46:03
使用 Java 访问 Windows 注册表(原创)

 

2005/5/24

使用 Java 访问 Windows 注册表

当然了, 只能运行在 Windows 下面, 可以使用开源的组件(很小, 大概40KB), JRegistryKey released under LGPL at http://www.sf.net/projects/jregistrykey.

下面是例子:

import ca.beq.util.win32.registry.*;

import java.util.*;

public class RegTest
{

 public RegTest()
    {
     RegistryKey r = new RegistryKey(RootKey.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");

  System.out.println(r);

  if(r.hasValue("Common Desktop")) {
     RegistryValue v = r.getValue("Common Desktop");
     System.out.println("Common Desktop = " + v.getStringValue());
  }

//  if(r.hasValues()) {
//     Iterator i = r.values();
//     while(i.hasNext()) {
//        RegistryValue v = (RegistryValue)i.next();
//        System.out.println(v.toString());
//     } // while
//  } // if
 }

 public static void main(String[] args)
    {
  RegTest regtest = new RegTest();
 }

}

这个程序可以读出来用户桌面所在的位置. 其实还有其他的用途, 例如 CLASSPATH 的设置一直都是让用户打开我得电脑 -> 属性云云, 很麻烦, 可以直接写程序来设置, 如下所示:

import ca.beq.util.win32.registry.*;
import java.io.*;

/**
 * Using JRegistryKey released under LGPL at http://www.sf.net/projects/jregistrykey
 * to access Windows registry to setup system variables on Win2K, WinXP platform.
 * DO NOT RUN THIS PROGRAM OTHERTHAN WINDOWS!
 *
 * Two variables need to be modified:
 * 1. CLASSPATH
 * 2. PATH
 *
 * @author BeanSoft
 * @version 0.1
 *
 */
public class Win32SystemVariableSetup
{

 public Win32SystemVariableSetup()
    {
  String logText = "";

  RegistryKey envReg = new RegistryKey(
   RootKey.HKEY_CURRENT_USER, "Environment");

  if(envReg.hasValue("CLASSPATH_BACKUP")) {
           logText += "It looks that classpath has been setted, not need to setup again!\n";
  }
  else {
   String originalClasspath = "";
   if(envReg.hasValue("CLASSPATH")) {
    originalClasspath = envReg.getValue("CLASSPATH").getStringValue();
    logText += "The original CLASSPATH is:" +
     originalClasspath + "\n";
    logText += "Now save it to a copy called CLASSPATH_BACKUP\n";

 //   RegistryKey backClasspathKey = new RegistryKey("CLASSPATH_BACKUP");
    RegistryValue backValue= new RegistryValue("CLASSPATH_BACKUP", originalClasspath);
 //   backClasspathKey.setValue(
             envReg.setValue(backValue);
   }

   // Setup new classpath
   String jmfClassPath = ".\\lib;.\\lib\\customizer.jar;.\\lib\\jmf.jar;.\\lib\\sound.jar;.\\lib\\mediaplayer.jar;.\\lib\\multiplayer.jar;.;";
   String beanChatClassPath = ".\\classes;.\\lib\\OfficeLnFs.jar;.\\lib\\xplookandfeel.jar;.\\lib\\hsqldb.jar;.\\lib\\jdbc_conn_pool.jar;";
   String newClassPathToSet = beanChatClassPath + jmfClassPath + originalClasspath;

         RegistryValue newClasspathValue = new RegistryValue("CLASSPATH", newClassPathToSet);
         logText += "New class path need to be setted:\n" + newClassPathToSet + "\n";
         envReg.setValue(newClasspathValue);
  }

  if(envReg.hasValue("PATH_BACKUP")) {
           logText += "It looks that path has been setted, not need to setup again!\n";
  }
  else {
   String originalPath = "";
   if(envReg.hasValue("PATH")) {
    originalPath = envReg.getValue("PATH").getStringValue();
    logText += "The original PATH is:" +
     originalPath + "\n";
    logText += "Now save it to a copy called PATH_BACKUP\n";

 //   RegistryKey backClasspathKey = new RegistryKey("CLASSPATH_BACKUP");
    RegistryValue backValue= new RegistryValue("PATH_BACKUP", originalPath);
 //   backClasspathKey.setValue(
             envReg.setValue(backValue);
   }

   // Setup new classpath
   String jmfPath = ".\\lib;.\\JMF_win32\\lib;.;";
   String newPathToSet = jmfPath + originalPath;

         RegistryValue newPathValue = new RegistryValue("PATH", newPathToSet);
         logText += "New path need to be setted:\n" + newPathToSet + "\n";
         envReg.setValue(newPathValue);
  }

  logText += "To make affect of the registry work, please log off and log on again or just reboot your computer.";
        javax.swing.JOptionPane.showMessageDialog(null, new java.awt.TextArea(
   logText));
        System.exit(0);
 }

 public static void main(String[] args)
    {
  Win32SystemVariableSetup win32systemvariablesetup = new Win32SystemVariableSetup();
 }

}