木木甫 2017-02-25 06:43 采纳率: 35.7%
浏览 963
已采纳

java创建注册表失败,????

 /**
 * Created by demo on 2017/2/25.
 */
import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
/**
 * @author solo L
 *
 */
class JNIRegistryTest {

    public  void openRegistry(){
        //打开注册表项并读出相应的值
        try {
            RegistryKey software = Registry.HKEY_LOCAL_MACHINE.
                    openSubKey("SOFTWARE");
            RegistryKey subKey = software.openSubKey("ymbizhi");
            String subKey1Value = subKey.getStringValue("Channel");
            String subKey2Value = subKey.getStringValue("InstallTime");
            System.out.println(subKey1Value);
            System.out.println(subKey2Value);
            subKey.closeKey();
        } catch (NoSuchKeyException e) {
            e.printStackTrace();
        } catch (RegistryException e) {
            e.printStackTrace();
        }
    }
    public void createRegist(){
        //创建注册表项并设置相应的值
        try {
            RegistryKey software = Registry.HKEY_LOCAL_MACHINE
                    .openSubKey("SOFTWARE");
            RegistryKey subKey = software.createSubKey("SubKeyName", "");
            subKey.setValue(new RegStringValue(subKey, "subKey1",
                    "subKey1Value"));
            subKey.setValue(new RegStringValue(subKey, "subKey2",
                    "subKey2Value"));
            subKey.closeKey();
        } catch (NoSuchKeyException e) {
            e.printStackTrace();
        } catch (RegistryException e) {
            e.printStackTrace();
        }
    }

}

单元测试:

 import org.junit.Test;

import static org.junit.Assert.*;

/**
 * Created by demo on 2017/2/25.
 */
public class JNIRegistryTestTest {

    private JNIRegistryTest regist = new JNIRegistryTest();

    @Test
    public void openRegistry() throws Exception {
        regist.openRegistry();
    }

    @Test
    public void createRegist() throws Exception {
        regist.createRegist();
    }

}

图片说明

  • 写回答

3条回答 默认 最新

  • 木木甫 2017-02-25 08:45
    关注

    最后我换了种方法,用的是java.util.Prefs解决的注册表创建问题,com.ice只能读,而不能创建。因为权限

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型