=============================================================
【注】
注册表非常非常重要,如果操作不当,极有可能使系统崩溃。所以在操作之前,请备份完整的注册表数据。以备不时之需。
==============================================================
此方法适合于读取系统生成的注册表节点,不适用于读取软件安装时生成的注册表节点
==============================================================
在读取注册表之前,我们需要把读取注册表的工具引入项目中,该工具的下载地址是:
下载完成之后,把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---->查看详细内容
================================================================