Posted on 2009-07-20 09:38
java人生 閱讀(2255)
評論(0) 編輯 收藏 所屬分類:
javaSE
需要修改的注冊表項
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run] 開機自動運行程序
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce] 開機自動運行程序 且 僅運行一次
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices] 開機自動運行服務
JDK 從1.4開始提供操作 Windows 的 API 是 Preferences,因為這個 API 也是跨平臺的,所功能比較弱,在 Win32 下只能用來操作 HKCU\Software\JavaSoft 和 HKLM\Software\JavaSoft 下及子節(jié)點的數(shù)據(jù)。
自由訪問注冊表其他鍵的值光用 Java 是做不到的,必然方案就是 JNI,這里我使用的是Windows Registry API Native Interface http://www.trustice.com/java/jnireg/index.shtml 下的 registry-3.1.3.zip(包含源代碼)。可以利用它訪問、修改、導出注冊表項到文件等。解開 registry-3.1.3.zip,在 bin 目錄中可以看到兩個文件 ICE_JNIRegistry.dll 和 registry.jar,動態(tài)庫就是本地代碼實現(xiàn)。
com.ice.jni.registry.Registry.main() 就是 registry 的示例代碼,動態(tài)庫 ICE_JNIRegistry.dll 也是在這個類的靜態(tài)塊中被加載的,記得要把 ICE_JNIRegistry.dll 放在它能夠被加載的位置上,比如你把 registry-3.1.3.zip 解壓到 c:\registry-3.1.3,在命令行下你可以進入到這個目錄中,并執(zhí)行。
代碼:

package org.zh.ss.util;

import com.ice.jni.registry.*;
import java.text.SimpleDateFormat;


/** *//**
* java 操作注冊表
* @author 李志遠
*/

public class RegeditTool
{

static SimpleDateFormat shortDateFormat = new SimpleDateFormat("yyyy-MM-dd");


/** *//** */

/** *//** Creates a new instance of test */

// 把信息存儲到注冊表HKEY_LOCAL_MACHINE下的某個節(jié)點的某一變量中,有則修改,無則創(chuàng)建
public static boolean setValue(String folder, String subKeyNode,

String subKeyName, String subKeyValue)
{

try
{
RegistryKey software = Registry.HKEY_LOCAL_MACHINE
.openSubKey(folder);
RegistryKey subKey = software.createSubKey(subKeyNode, "");
subKey
.setValue(new RegStringValue(subKey, subKeyName,
subKeyValue));
subKey.closeKey();
return true;

} catch (NoSuchKeyException e)
{
e.printStackTrace();

} catch (NoSuchValueException e)
{
e.printStackTrace();

} catch (RegistryException e)
{
e.printStackTrace();
}
return false;
}

// 刪除注冊表中某節(jié)點下的某個變量
public static boolean deleteValue(String folder, String subKeyNode,

String subKeyName)
{

try
{
RegistryKey software = Registry.HKEY_LOCAL_MACHINE
.openSubKey(folder);
RegistryKey subKey = software.createSubKey(subKeyNode, "");
subKey.deleteValue(subKeyName);
subKey.closeKey();
return true;

} catch (NoSuchKeyException e)
{
System.out.println("NOsuchKey_delete");

} catch (NoSuchValueException e)
{
System.out.println("NOsuchValue_delete");

} catch (RegistryException e)
{
e.printStackTrace();
}
return false;
}

// 刪除注冊表中某節(jié)點下的某節(jié)點

public static boolean deleteSubKey(String folder, String subKeyNode)
{

try
{
RegistryKey software = Registry.HKEY_LOCAL_MACHINE
.openSubKey(folder);
software.deleteSubKey(subKeyNode);
software.closeKey();
return true;

} catch (NoSuchKeyException e)
{
e.printStackTrace();

} catch (RegistryException e)
{
e.printStackTrace();
}
return false;
}

// 打開注冊表項并讀出相應的變量名的值
public static String getValue(String folder, String subKeyNode,

String subKeyName)
{
String value = "";

try
{
RegistryKey software = Registry.HKEY_LOCAL_MACHINE
.openSubKey(folder);
RegistryKey subKey = software.openSubKey(subKeyNode);
value = subKey.getStringValue(subKeyName);
subKey.closeKey();

} catch (NoSuchKeyException e)
{
value = "NoSuchKey";
// e.printStackTrace();

} catch (NoSuchValueException e)
{
value = "NoSuchValue";
// e.printStackTrace();

} catch (RegistryException e)
{
e.printStackTrace();
}
return value;
}

// 測試

public static void main(String[] args)
{
setValue("SOFTWARE", "Microsoft\\Windows\\CurrentVersion\\Run", "test",
"C:\\1.exe");
}
}