Java读写注册表两种方式

36 篇文章 0 订阅

一种是使用java提供的,但是只能获取HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs下的

 

另外一种是可以指定任意路径,因为采用的是cmd的命令。

package com.reg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

public class Registery {

    String[] keys = { "version", "initial", "creator" };
    String[] values = { "1.3", "ini.mp3", "caokai1818@sina.com" };

    // 把相应的值储存到变量中去
    public void writeValue() {
        // HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs下写入注册表值.
        Preferences pre = Preferences.systemRoot().node("/javaplayer");
        for (int i = 0; i < keys.length; i++) {
            pre.put(keys[i], values[i]);
        }
    }

    /***
     * 根据key获取value
     * 
     */
    public String getValue(String key) {
        Preferences pre = Preferences.systemRoot().node("/javaplayer");
        return pre.get(key, "time");
    }

    /***
     * 清除注册表
     * 
     * @throws BackingStoreException
     */
    public void clearValue() throws BackingStoreException {
        Preferences pre = Preferences.systemRoot().node("/javaplayer");
        pre.clear();
    }

    public static void main(String[] args) {
        Registery reg = new Registery();
        reg.writeValue();
        System.out.println(reg.getValue("version"));
//        try {
//            reg.clearValue();
//        } catch (BackingStoreException e) {
//            e.printStackTrace();
//        }
        
        //可以读取任意路径下的、
        try {  
            Process ps = null;  
            ps = Runtime.getRuntime().exec("reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Prefs\\javaplayer");  
            ps.getOutputStream().close();  
            InputStreamReader i = new InputStreamReader(ps.getInputStream());  
            String line;  
            BufferedReader ir = new BufferedReader(i);  
            while ((line = ir.readLine()) != null) {  
                System.out.println(line);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
使用微软提供的CReg类可以方便地读写注册表,具体用法如下: ```cpp #include <creg.hxx> // 读取注册表 CRegKey reg; if (reg.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"), KEY_READ) == ERROR_SUCCESS) { DWORD dwType = REG_SZ; TCHAR szValue[1024] = { 0 }; ULONG ulCount = 1024; if (reg.QueryStringValue(_T("ProgramFilesDir"), szValue, &ulCount) == ERROR_SUCCESS) { // 读取成功 // szValue中存储了ProgramFilesDir的值 } reg.Close(); } // 写入注册表 CRegKey reg; if (reg.Create(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\MyCompany\\MyApp")) == ERROR_SUCCESS) { if (reg.SetStringValue(_T("MyValue"), _T("Hello, World!")) == ERROR_SUCCESS) { // 写入成功 } reg.Close(); } ``` 另外,在VB中,可以使用自身的SaveSetting、GetSetting函数读写注册表,也可以使用API函数来实现注册表任意读写。具体用法可以参考以下代码: ```vb ' 读取注册表 Dim hKey As Long Dim dwType As Long Dim dwSize As Long Dim sValue As String hKey = OpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion", KEY_READ) If hKey <> 0 Then dwType = REG_SZ sValue = String$(1024, 0) dwSize = Len(sValue) If QueryValueEx(hKey, "ProgramFilesDir", 0, dwType, ByVal sValue, dwSize) = ERROR_SUCCESS Then ' 读取成功 ' sValue中存储了ProgramFilesDir的值 End If RegCloseKey hKey End If ' 写入注册表 hKey = CreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\MyCompany\MyApp") If hKey <> 0 Then If SetValueEx(hKey, "MyValue", 0, REG_SZ, "Hello, World!", Len("Hello, World!")) = ERROR_SUCCESS Then ' 写入成功 End If RegCloseKey hKey End If ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值