Java读取注册表总结之读取的简单方法

=============================================================

【注】

注册表非常非常重要,如果操作不当,极有可能使系统崩溃。所以在操作之前,请备份完整的注册表数据。以备不时之需。

  备份方法

==============================================================

此方法适合于读取系统生成的注册表节点,不适用于读取软件安装时生成的注册表节点

==============================================================

在读取注册表之前,我们需要把读取注册表的工具引入项目中,该工具的下载地址是:

    点击打开

下载完成之后,把WinRegistry.java引入项目。如下图所示

下面我们在RegistryCutTest.java中写我们的代码。

1)获取指定节点下指定key的value

代码:

        /**
	 * 获取指定节点下指定key的value<br>
	 * 指定的节点是 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion<br>
	 * 指定的key是 ProductName
	 * 
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	private static void Test1() throws Exception {
		String value = WinRegistry.readString(WinRegistry.HKEY_LOCAL_MACHINE, // HKEY
				"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", // Key
				"ProductName"); // ValueName
		System.out.println("Windows Distribution = " + value);
	}
执行结果:

Windows Distribution = Windows 7 Ultimate
============================================================

2)获取指定节点下所有的键值对

代码:

        /**
	 * 获取指定节点下所有的键值对<br>
	 * 指定的节点是 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion<br>
	 * 
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	private static void Test2() throws Exception {
		Map<String, String> m = WinRegistry.readStringValues(
				WinRegistry.HKEY_LOCAL_MACHINE, // HKEY
				"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"); // ValueName
		Set<Entry<String, String>> entrySet = m.entrySet();
		for (Entry<String, String> entry : entrySet) {
			System.out.println(entry.getKey() + "=" + entry.getValue());
		}
	}
执行结果:

CurrentVersion=6.1
CurrentBuild=7601
SoftwareType=System
CurrentType=Multiprocessor Free
InstallDate=null
RegisteredOrganization=FBI
RegisteredOwner=Xiaofeng
SystemRoot=C:\windows
InstallationType=Client
EditionID=Ultimate
ProductName=Windows 7 Ultimate
CurrentBuildNumber=7601
BuildLab=7601.win7sp1_gdr.140706-1506
BuildLabEx=7601.18526.amd64fre.win7sp1_gdr.140706-1506
BuildGUID=a05e5854-a001-4c22-8851-dd785e1048d8
CSDBuildNumber=1130
PathName=C:\Windows
ProductId=00426-065-4868673-86048
SourcePath=C:\Windows\system
====================================================

3)在有权限的情况下,获取指定节点下所有的子节点

代码:

	/**
	 * 在有权限的情况下,获取指定节点下所有的子节点
	 * 
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * 
	 * @throws Exception
	 */
	private static void Test3() throws Exception {
		List<String> subKeys = WinRegistry.readStringSubKeys(
				WinRegistry.HKEY_LOCAL_MACHINE,
				 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
		if (subKeys == null) {
			System.out.println("null1");
			return;
		}
		for (int i = 0; i < subKeys.size(); i++) {
			if (subKeys.get(i) == null) {
				System.out.println("空");
			} else {
				System.out.println(subKeys.get(i));
			}
		}
	}
执行结果:

Accessibility
AdaptiveDisplayBrightness
AeDebug
APITracing
AppCompatFlags
ASR
Audit
BootMgr
Compatibility32
DeviceDisplayObjects
drivers.desc
Drivers32
EFS
Event Viewer
Font Drivers
ICM
IniFileMapping
KnownFunctionTableDlls
KnownManagedDebuggingDlls
MCI Extensions
MCI32
MiniDumpAuxiliaryDlls
NetworkList
NtVdm64
OpenGLDrivers
PeerNet
PerHwIdStorage
ProfileLoader
ProfileNotification
Schedule
SeCEdit
SoftwareProtectionPlatform
Svchost
SystemRestore
Tracing
WbemPerf
Windows
Winlogon
Winsat
Console
FontDPI
FontLink
FontMapper
Fonts
FontSubstitutes
GRE_Initialize
Image File Execution Options
LanguagePack
NetworkCards
Perflib
Ports
Print
ProfileList
Time Zones
【注】之所以说是在有权限的情况下,是因为有时不能获取全部的子节点。

======================================================


以上这种方法在读取系统节点时非常方便。



================================================================
Java读取注册表的三种方法----------------------->查看详细内容
Java读取注册表总结之Java API----------------->查看详细内容
Java读取注册表总结之com.ice.jni.registry---->查看详细内容
================================================================

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值