java 删除注册表_java对windows注册表进行增删查

[java]代码库package info.itlanguageexpress.java;

import java.io.*;

public class RegKeyManager {

private final String TYPES[] = { "SZ", "BINARY", "DWORD", "QWORD", "DWORD_LITTLE_ENDIAN", "QWORD_LITTLE_ENDIAN", "DWORD_BIG_ENDIAN", "EXPAND_SZ", "LINK", "MULTI_SZ", "NONE", "RESOURCE_LIST" };

private String type = "", value = "", key = "";

protected void query(String loc, String k) throws Exception {

Process p = Runtime.getRuntime().exec("reg QUERY \"" + loc + "\" /v \"" + k + "\"");

BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );

String out = "";

while ( ( out = in.readLine() ) != null ) {

if (out.matches("(.*)\\s+REG_(.*)")) { break; }

}

in.close();

String str[] = out.split(" ");

int b = 0;

for (int a=0; a < str.length; a++) {

if ( str[a].replace(" ", "").matches("\\S+") ) {

switch (b) {

case 0: key = str[a]; break;

case 1: type = str[a]; break;

case 2: value = str[a]; break;

}

b++;

}

}

}

protected String getKey() { return key; }

protected String getType() { return type; }

protected String getValue() { return value; }

protected boolean add(String loc, String name, String dType, String value) throws Exception {

boolean comp = false, valid = false;

for (int a = 0; a < TYPES.length; a++) {

if (dType.equalsIgnoreCase("REG_" + TYPES[a])) { valid = true; break; }

}

if ( valid ) {

Process p = Runtime.getRuntime().exec("reg ADD \"" + loc + "\" /v \"" + name + "\" /t \"" + dType + "\" /d \"" + value + "\"");

BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );

String out = "";

while ( (out = in.readLine() ) != null ) {

if (out.equalsIgnoreCase("The operation completed successfully.")) { comp = true; }

}

in.close();

}

return comp;

}

protected boolean delete(String loc, String key) throws Exception {

boolean comp = false;

Process p = Runtime.getRuntime().exec("reg DELETE \"" + loc + "\" /v \"" + key + "\" /f");

BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );

String out = "";

while ( ( out = in.readLine() ) != null ) {

if (out.equalsIgnoreCase("The operation completed successfully.")) { comp = true; }

}

in.close();

return comp;

}

}

//源代码片段来自云代码http://yuncode.net

public static void main(String[] args) {

try {

RegKeyManager rkm = new RegKeyManager();

// 64位系统的注册表目录

// rkm.query("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Tencent\\QQ2009", "Install");

// 32位系统的注册表目录

rkm.query("HKEY_LOCAL_MACHINE\\SOFTWARE\\Tencent\\QQ2009", "Install");

System.out.println("KEY: " + rkm.getKey() + " DATA TYPE: " + rkm.getType() + " DATA VALUE: " + rkm.getValue());

// rkm.add("HKEY_LOCAL_MACHINE\\SOFTWARE\\Tencent\\QQ2009","TESTING","REG_SZ","VALUE DATA");

// rkm.delete("HKEY_LOCAL_MACHINE\\SOFTWARE\\Tencent\\QQ2009","TESTING");

} catch (Exception e) {

e.printStackTrace();

}

}

//源代码片段来自云代码http://yuncode.net

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值