谢谢大家!
今天我读到了一位朋友sudianbo84的邮件,照抄如下:
Dear Mr solol,
I am sorry to send this email to bother you due to my stupidity,however ,I really need your help .
Imitating the main() method provided by yours JNIRegistry Package , I wrote a program to modify
the Internet Explore's settings in the windows' Registry ; but with something wrong when I run my
program,I hope you can give me some help or advices! the following is the hints when my program runs
"com.ice.jni.registry.RegistryException: Registry API Error 5, 'access denied' - 'RegSetValueEx()'
at com.ice.jni.registry.RegistryKey.setValue(Native Method)
at org.solol.test.ssss.main(ssss.java:29)
com.ice.jni.registry.NoSuchValueException: RegQueryValueEx(), value='ProxyServer'
at com.ice.jni.registry.RegistryKey.getStringValue(Native Method)
at org.solol.test.ssss.main(ssss.java:47)
Press any key to continue..."
And this Is my program list:(I hope you can give me some help or advices! )
package org.solol.test;
import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.RegDWordValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
import com.ice.jni.registry.RegistryValue;
/**
* @author solo L
*
*/
public class ssss {
/**
* @param args
*/
public static void main(String[] args) {
try {
RegistryKey IEsetting = Registry.HKEY_CURRENT_USER.openSubKey("Software").openSubKey("Microsoft").openSubKey("Windows").openSubKey("CurrentVersion").openSubKey("Internet Settings");
IEsetting.setValue("ProxyServer", new RegStringValue(IEsetting,"ProxyServer","127.0.0.1:8080"));
IEsetting.setValue(new RegDWordValue(IEsetting,"ProxyEnable", RegistryValue.REG_DWORD,1));
IEsetting.closeKey();
} catch (NoSuchKeyException e) {
e.printStackTrace();
} catch (RegistryException e) {
e.printStackTrace();
}
try {
RegistryKey IEsetting = Registry.HKEY_CURRENT_USER.openSubKey("Software").openSubKey("Microsoft").openSubKey("Windows").openSubKey("CurrentVersion").openSubKey("Internet Settings");
//RegistryKey IEsetting = software.openSubKey("SubKeyName");
RegistryValue proxyenable = IEsetting.getValue("ProxyEnable");
String proxyserver = IEsetting.getStringValue("ProxyServer");
System.out.println(proxyenable.toString());
System.out.println(proxyserver);
IEsetting.closeKey();
} catch (NoSuchKeyException e) {
e.printStackTrace();
} catch (RegistryException e) {
e.printStackTrace();
}
}
}
这是由于我在文章用Java操作Windows注册表中犯了一个错误。
出错内容为:
RegistryKey openSubKey(java.lang.String subkey) 打开该key的subkey,具有写权限。
应该为:
RegistryKey openSubKey(java.lang.String subkey) 打开该key的subkey,具有读权限。
我已经对文章进行了更新。
sudianbo84朋友要解决你的问题可以使用下面的代码:
RegistryKey currentVersion = Registry.HKEY_CURRENT_USER.openSubKey("Software").openSubKey("Microsoft").openSubKey("Windows").openSubKey("CurrentVersion");
RegistryKey inetSetting = currentVersion.openSubKey("Internet Settings",RegistryKey.ACCESS_WRITE);
inetSetting.setValue(new RegStringValue(inetSetting, "ProxyServer", "127.0.0.1:8080"));
inetSetting.setValue(new RegDWordValue(inetSetting, "ProxyEnable",RegistryValue.REG_DWORD, 1));
再次感谢大家!谢谢!