<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    xhchc

    危波帆墻,笑談只在桃花上;與誰共尚,風吹萬里浪; 相依相偎,不做黃泉想;莫惆悵,碧波潮生,一蕭自狂放……

     

    Java中操作Windows注冊表的代碼示例

    想做個東西,要獲IE的代理設置,看網上介紹基本都是讀取注冊表的方式,沒提到說借助于特定的 Win32 API。而 JDK 提供操作 Windows 的 API 也就是 Preferences,因為這個 API 也是跨平臺的,所功能比較弱,在 Win32 下只能用來操作 HKCUSoftwareJavaSoft 和 HKLMSoftwareJavaSoft 下及子節點的數據。

    自由訪問注冊表其他鍵的值光用 Java 是做不到的,必然方案就是 JNI,一開始也自己來實現這個 JNI 動態庫,后來懶了一下,想著網上應該用現成的實現,Google 了一下,果然不出所望,就是 http://www.trustice.com/java/jnireg/index.shtml 下的 registry-3.1.3.zip(包含源代碼)。可以利用它訪問、修改、導出注冊表項到文件等。解開 registry-3.1.3.zip,在 bin 目錄中可以看到兩個文件 ICE_JNIRegistry.dll 和 registry.jar,動態庫就是本地代碼實現。

    com.ice.jni.registry.Registry.main() 就是 registry 的示例代碼,動態庫 ICE_JNIRegistry.dll 也是在這個類的靜態塊中被加載的,記得要把 ICE_JNIRegistry.dll 放在它能夠被加載的位置上,比如你把 registry-3.1.3.zip 解壓到 c:registry-3.1.3,在命令行下你可以進入到這個目錄中,并執行

    c:registry-3.1.3>java -cp registry.jar com.ice.jni.registry.Registry ////ICE_JNIRegistry.dll 這個東西不知道放在哪,這個例子是我從網上找的,沒弄出來

    就可以在 command: 提示符下輸入命令操作注冊表了,輸入 help 會打印出所有能用指令:

    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 是否啟用代理設置就可以輸入指令

     

    command: data "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" ProxyEnable

     

    顯示結果為:

     

    Value 'ProxyEnable' is [type=4,name=ProxyEnable]
    REG_DWORD '0' [x00000000]

     

    '0' 表示未啟用 IE 代理設置,如果啟用了代理設置,通過其他幾個字符串值可獲取到具體的代理設置。如

    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'

    上面用了一個內置變量 %6 來代表 HKCUSoftwareMicrosoft。

     

    其他的指令,大家可以嘗試一下,這里我主要是關注于讀取鍵值的操作,接下來呢,我們來看看自己寫代碼如何來獲取鍵值。應好好的參考 Registry 類的實現。

     

     

    1. package  com.unmi;   
    2.   
    3. import  com.ice.jni.registry.*;   
    4.   
    5. /**  
    6.  * @author Unmi  
    7.  */   
    8. public   class  RegistryManager   
    9. {   
    10.   
    11.      /**  
    12.      * @param args  
    13.      * @throws RegistryException   
    14.      * @throws NoSuchKeyException   
    15.      */   
    16.      public   static   void  main(String[] args)  throws  NoSuchKeyException, RegistryException   
    17.     {   
    18.         RegistryKey registryKey = Registry.openSubkey(Registry.HKEY_CURRENT_USER,   
    19.               "Software\Microsoft\Windows\CurrentVersion\Internet Settings" ,   
    20.              RegistryKey.ACCESS_READ);   
    21.         RegistryValue registryValue = registryKey.getValue( "ProxyEnable" );   
    22.          boolean  proxyEnable = ((RegDWordValue)registryValue).getData()!=  0 ;   
    23.            
    24.         System.out.println( "IE 是否啟用了代理設置: " +proxyEnable);   
    25.            
    26.          if (proxyEnable)   
    27.         {   
    28.             registryValue = registryKey.getValue( "ProxyServer" );   
    29.             System.out.println( "IE 代理服務器是: " + new  String(registryValue.getByteData()));   
    30.         }   
    31.     }   
    32. }  

     

    我在 IE 中為不同協議設置了代理后,執行上面程序的輸出為(如果要取出 http 代理設置就分隔進行字符串處理了):

     

    IE 是否啟用了代理設置: true
    IE 代理服務器是: ftp=proxy.unmi.com:21;gopher=proxy.unmi.com:8080;http=proxy.unmi.com:8080;https=proxy.unmi.com:8080

    如果對所有服務器均使用相同的代理服務器設置后,執行上面程序的輸出為(默認就是 http 代理設置了):

     

    IE 是否啟用了代理設置: true
    IE 代理服務器是: proxy.unmi.com:80

    大家可以繼續挖掘 registry 的修改鍵值及創建子鍵的功能。

    posted on 2008-08-06 20:43 chu 閱讀(531) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆檔案

    我的鏈接

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产亚洲免费的视频看| 日本免费v片一二三区| 亚洲精品少妇30p| 在线免费播放一级毛片 | 免费看片免费播放| 亚洲日韩看片无码电影| 四虎影院免费在线播放| 亚洲国产成人AV网站| 国产精品免费看香蕉| 免费中文字幕视频| 国产亚洲精品影视在线产品| 插鸡网站在线播放免费观看| 国产亚洲一区二区手机在线观看 | 中文字幕亚洲一区二区va在线| 一个人免费播放在线视频看片| 伊人久久精品亚洲午夜| a级特黄毛片免费观看| 亚洲综合激情另类小说区| 成年在线观看网站免费| 成人婷婷网色偷偷亚洲男人的天堂| 夜色阁亚洲一区二区三区| 最近国语视频在线观看免费播放| 亚洲国产精品lv| 成人毛片免费播放| 国产成人精品免费大全| 亚洲国产精品yw在线观看| 日韩午夜免费视频| 国色精品va在线观看免费视频 | 亚洲&#228;v永久无码精品天堂久久 | 亚洲国产精品无码第一区二区三区| 免费a级毛片网站| 欧洲精品99毛片免费高清观看| 亚洲一区免费视频| 国产亚洲精午夜久久久久久| 一区二区三区四区免费视频| 亚洲欧美国产日韩av野草社区| 亚洲精品黄色视频在线观看免费资源 | 成年美女黄网站18禁免费 | 亚洲人成电影网站久久| 久久久久亚洲AV成人网人人软件| 日韩免费无码一区二区三区|