1. 程式人生 > >Java讀取登錄檔總結之讀取的簡單方法

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---->
================================================================