Java中操作Windows注冊(cè)表的代碼示例
想做個(gè)東西,要獲IE的代理設(shè)置,看網(wǎng)上介紹基本都是讀取注冊(cè)表的方式,沒(méi)提到說(shuō)借助于特定的 Win32 API。而 JDK 提供操作 Windows 的 API 也就是 Preferences,因?yàn)檫@個(gè) API 也是跨平臺(tái)的,所功能比較弱,在 Win32 下只能用來(lái)操作 HKCUSoftwareJavaSoft 和 HKLMSoftwareJavaSoft 下及子節(jié)點(diǎn)的數(shù)據(jù)。
自由訪問(wèn)注冊(cè)表其他鍵的值光用 Java 是做不到的,必然方案就是 JNI,一開(kāi)始也自己來(lái)實(shí)現(xiàn)這個(gè) JNI 動(dòng)態(tài)庫(kù),后來(lái)懶了一下,想著網(wǎng)上應(yīng)該用現(xiàn)成的實(shí)現(xiàn),Google 了一下,果然不出所望,就是 http://www.trustice.com/java/jnireg/index.shtml 下的 registry-3.1.3.zip(包含源代碼)。可以利用它訪問(wèn)、修改、導(dǎo)出注冊(cè)表項(xiàng)到文件等。解開(kāi) registry-3.1.3.zip,在 bin 目錄中可以看到兩個(gè)文件 ICE_JNIRegistry.dll 和 registry.jar,動(dòng)態(tài)庫(kù)就是本地代碼實(shí)現(xiàn)。
com.ice.jni.registry.Registry.main() 就是 registry 的示例代碼,動(dòng)態(tài)庫(kù) ICE_JNIRegistry.dll 也是在這個(gè)類(lèi)的靜態(tài)塊中被加載的,記得要把 ICE_JNIRegistry.dll 放在它能夠被加載的位置上,比如你把 registry-3.1.3.zip 解壓到 c:registry-3.1.3,在命令行下你可以進(jìn)入到這個(gè)目錄中,并執(zhí)行
c:registry-3.1.3>java -cp registry.jar com.ice.jni.registry.Registry ////ICE_JNIRegistry.dll 這個(gè)東西不知道放在哪,這個(gè)例子是我從網(wǎng)上找的,沒(méi)弄出來(lái)
就可以在 command: 提示符下輸入命令操作注冊(cè)表了,輸入 help 會(huì)打印出所有能用指令:
keys regKey -- print the key names
values regKey -- print the value names
data regKey subKey -- print the key's data
string regKey subKey -- print REG_SZ key's string
setbin regKey subKey binaryString -- set REG_BINARY
setdw regKey subKey int -- set REG_DWORD
setstr regKey subKey string -- set REG_SZ
setmulti regKey subKey semiColonString -- set REG_MULTI_SZ
delkey regKey subKey -- delete key 'subKey' of regKey
delval regKey subKey -- delete value 'subKey' of regKey
export regKey fileName -- export registry key to fileName
expand regKey valueName -- expand string value
!! -- repeats last command
$$ -- re-uses previous keyname
Predefined Key Prefixes: (e.g. $0-9)
$0=HKLMSystemCurrentControlSetcontrol
$1=HKLMSoftware
$2=HKLMSoftwareMiscrosoft
$3=HKLMSoftwareMicrosoftWindowsCurrentVersion
$4=HKLMSoftwareMicrosoftWindowsCurrentVersionProfileList
$5=HKCUSoftware
$6=HKCUSoftwareMicrosoft
$7=HKCUAppEvents
$8=HKCUAppEventsSchemes
$9=HKCUAppEventsSchemes
比如要讀取 IE 是否啟用代理設(shè)置就可以輸入指令
command: data "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" ProxyEnable
顯示結(jié)果為:
Value 'ProxyEnable' is [type=4,name=ProxyEnable]
REG_DWORD '0' [x00000000]
'0' 表示未啟用 IE 代理設(shè)置,如果啟用了代理設(shè)置,通過(guò)其他幾個(gè)字符串值可獲取到具體的代理設(shè)置。如
command: data "%6\Windows\CurrentVersion\Internet Settings\" ProxyServer
Value 'ProxyServer' is [type=1,name=ProxyServer]
REG_SZ 'ftp=proxy.unmi.com:21;gopher=proxy.unmi.com:8080;http=proxy.unmi.com:8080;https=proxy.unmi.com:8080'
上面用了一個(gè)內(nèi)置變量 %6 來(lái)代表 HKCUSoftwareMicrosoft。
其他的指令,大家可以嘗試一下,這里我主要是關(guān)注于讀取鍵值的操作,接下來(lái)呢,我們來(lái)看看自己寫(xiě)代碼如何來(lái)獲取鍵值。應(yīng)好好的參考 Registry 類(lèi)的實(shí)現(xiàn)。
- package com.unmi;
- import com.ice.jni.registry.*;
- /**
- * @author Unmi
- */
- public class RegistryManager
- {
- /**
- * @param args
- * @throws RegistryException
- * @throws NoSuchKeyException
- */
- public static void main(String[] args) throws NoSuchKeyException, RegistryException
- {
- RegistryKey registryKey = Registry.openSubkey(Registry.HKEY_CURRENT_USER,
- "Software\Microsoft\Windows\CurrentVersion\Internet Settings" ,
- RegistryKey.ACCESS_READ);
- RegistryValue registryValue = registryKey.getValue( "ProxyEnable" );
- boolean proxyEnable = ((RegDWordValue)registryValue).getData()!= 0 ;
- System.out.println( "IE 是否啟用了代理設(shè)置: " +proxyEnable);
- if (proxyEnable)
- {
- registryValue = registryKey.getValue( "ProxyServer" );
- System.out.println( "IE 代理服務(wù)器是: " + new String(registryValue.getByteData()));
- }
- }
- }
我在 IE 中為不同協(xié)議設(shè)置了代理后,執(zhí)行上面程序的輸出為(如果要取出 http 代理設(shè)置就分隔進(jìn)行字符串處理了):
IE 是否啟用了代理設(shè)置: true
IE 代理服務(wù)器是: ftp=proxy.unmi.com:21;gopher=proxy.unmi.com:8080;http=proxy.unmi.com:8080;https=proxy.unmi.com:8080
如果對(duì)所有服務(wù)器均使用相同的代理服務(wù)器設(shè)置后,執(zhí)行上面程序的輸出為(默認(rèn)就是 http 代理設(shè)置了):
IE 是否啟用了代理設(shè)置: true
IE 代理服務(wù)器是: proxy.unmi.com:80
大家可以繼續(xù)挖掘 registry 的修改鍵值及創(chuàng)建子鍵的功能。
posted on 2008-08-06 20:43 chu 閱讀(533) 評(píng)論(0) 編輯 收藏