如何使用 java 读取注册表值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6327482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:24:28  来源:igfitidea点击:

How can I read the registry values using java?

java

提问by amod

Possible Duplicate:
read/write to Windows Registry using Java

可能的重复:
使用 Java 读/写 Windows 注册表

I want to read the registry value of the path HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CurrentVersion\Uninstall\{2FC099BD-AC9B-33EB-809C-D332E1B27C40}.

我想读取路径的注册表值HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CurrentVersion\Uninstall\{2FC099BD-AC9B-33EB-809C-D332E1B27C40}

Can you please help me with the code?

你能帮我看看代码吗?

回答by HackToHell

This shows how to read the registry, but could be extended to write operations: How to read the Windows Registry

这显示了如何读取注册表,但可以扩展为写入操作:如何读取 Windows 注册表

import java.io.*;

public class RegQuery {

  private static final String REGQUERY_UTIL = "reg query ";
  private static final String REGSTR_TOKEN = "REG_SZ";
  private static final String REGDWORD_TOKEN = "REG_DWORD";

  private static final String PERSONAL_FOLDER_CMD = REGQUERY_UTIL +
    "\"HKCU\Software\Microsoft\Windows\CurrentVersion\"
     + "Explorer\Shell Folders\" /v Personal";
  private static final String CPU_SPEED_CMD = REGQUERY_UTIL +
    "\"HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\0\""
     + " /v ~MHz";
  private static final String CPU_NAME_CMD = REGQUERY_UTIL +
   "\"HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\0\""
     + " /v ProcessorNameString";

  public static String getCurrentUserPersonalFolderPath() {
    try {
      Process process = Runtime.getRuntime().exec(PERSONAL_FOLDER_CMD);
      StreamReader reader = new StreamReader(process.getInputStream());

      reader.start();
      process.waitFor();
      reader.join();

      String result = reader.getResult();
      int p = result.indexOf(REGSTR_TOKEN);

      if (p == -1)
         return null;

      return result.substring(p + REGSTR_TOKEN.length()).trim();
    }
    catch (Exception e) {
      return null;
    }
  }

  public static String getCPUSpeed() {
    try {
      Process process = Runtime.getRuntime().exec(CPU_SPEED_CMD);
      StreamReader reader = new StreamReader(process.getInputStream());

      reader.start();
      process.waitFor();
      reader.join();

      String result = reader.getResult();
      int p = result.indexOf(REGDWORD_TOKEN);

      if (p == -1)
         return null;

      // CPU speed in Mhz (minus 1) in HEX notation, convert it to DEC
      String temp = result.substring(p + REGDWORD_TOKEN.length()).trim();
      return Integer.toString
          ((Integer.parseInt(temp.substring("0x".length()), 16) + 1));
    }
    catch (Exception e) {
      return null;
    }
  }

  public static String getCPUName() {
    try {
      Process process = Runtime.getRuntime().exec(CPU_NAME_CMD);
      StreamReader reader = new StreamReader(process.getInputStream());

      reader.start();
      process.waitFor();
      reader.join();

      String result = reader.getResult();
      int p = result.indexOf(REGSTR_TOKEN);

      if (p == -1)
         return null;

      return result.substring(p + REGSTR_TOKEN.length()).trim();
    }
    catch (Exception e) {
      return null;
    }
  }

  static class StreamReader extends Thread {
    private InputStream is;
    private StringWriter sw;

    StreamReader(InputStream is) {
      this.is = is;
      sw = new StringWriter();
    }

    public void run() {
      try {
        int c;
        while ((c = is.read()) != -1)
          sw.write(c);
        }
        catch (IOException e) { ; }
      }

    String getResult() {
      return sw.toString();
    }
  }

  public static void main(String s[]) {
    System.out.println("Personal directory : "
       + getCurrentUserPersonalFolderPath());
    System.out.println("CPU Name : " + getCPUName());
    System.out.println("CPU Speed : " + getCPUSpeed() + " Mhz");
  }
}

回答by Buhake Sindi

There's a tutorialthat shows you without using Runtime.exec()function and uses specifically java.util.prefs.WindowsPreferences.

有一个教程向您展示了不使用Runtime.exec()函数并专门使用java.util.prefs.WindowsPreferences.