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

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条回答 默认 最新

相关推荐 更多相似问题