Posted on 2009-07-20 09:38
java人生 阅读(2245)
评论(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 下及子节点的数据。
自由访问注册表其他键的值光用 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,动态库就是本地代码实现。
com.ice.jni.registry.Registry.main() 就是 registry 的示例代码,动态库 ICE_JNIRegistry.dll 也是在这个类的静态块中被加载的,记得要把 ICE_JNIRegistry.dll 放在它能够被加载的位置上,比如你把 registry-3.1.3.zip 解压到 c:\registry-3.1.3,在命令行下你可以进入到这个目录中,并执行。
代码:
![](/Images/OutliningIndicators/None.gif)
package org.zh.ss.util;
![](/Images/OutliningIndicators/None.gif)
import com.ice.jni.registry.*;
import java.text.SimpleDateFormat;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
* java 操作注册表
* @author 李志远
*/
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class RegeditTool
{
![](/Images/OutliningIndicators/InBlock.gif)
static SimpleDateFormat shortDateFormat = new SimpleDateFormat("yyyy-MM-dd");
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//** */
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//** Creates a new instance of test */
![](/Images/OutliningIndicators/InBlock.gif)
// 把信息存储到注册表HKEY_LOCAL_MACHINE下的某个节点的某一变量中,有则修改,无则创建
public static boolean setValue(String folder, String subKeyNode,
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
String subKeyName, String subKeyValue)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (NoSuchKeyException e)
{
e.printStackTrace();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (NoSuchValueException e)
{
e.printStackTrace();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (RegistryException e)
{
e.printStackTrace();
}
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
// 删除注册表中某节点下的某个变量
public static boolean deleteValue(String folder, String subKeyNode,
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
String subKeyName)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
RegistryKey software = Registry.HKEY_LOCAL_MACHINE
.openSubKey(folder);
RegistryKey subKey = software.createSubKey(subKeyNode, "");
subKey.deleteValue(subKeyName);
subKey.closeKey();
return true;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (NoSuchKeyException e)
{
System.out.println("NOsuchKey_delete");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (NoSuchValueException e)
{
System.out.println("NOsuchValue_delete");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (RegistryException e)
{
e.printStackTrace();
}
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
// 删除注册表中某节点下的某节点
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static boolean deleteSubKey(String folder, String subKeyNode)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
RegistryKey software = Registry.HKEY_LOCAL_MACHINE
.openSubKey(folder);
software.deleteSubKey(subKeyNode);
software.closeKey();
return true;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (NoSuchKeyException e)
{
e.printStackTrace();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (RegistryException e)
{
e.printStackTrace();
}
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
// 打开注册表项并读出相应的变量名的值
public static String getValue(String folder, String subKeyNode,
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
String subKeyName)
{
String value = "";
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
RegistryKey software = Registry.HKEY_LOCAL_MACHINE
.openSubKey(folder);
RegistryKey subKey = software.openSubKey(subKeyNode);
value = subKey.getStringValue(subKeyName);
subKey.closeKey();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (NoSuchKeyException e)
{
value = "NoSuchKey";
// e.printStackTrace();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (NoSuchValueException e)
{
value = "NoSuchValue";
// e.printStackTrace();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (RegistryException e)
{
e.printStackTrace();
}
return value;
}
![](/Images/OutliningIndicators/InBlock.gif)
// 测试
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
setValue("SOFTWARE", "Microsoft\\Windows\\CurrentVersion\\Run", "test",
"C:\\1.exe");
}
}