IT/RCP

[SWT] 윈도우 레지스트리

원창연 2020. 12. 19. 23:52

레지스트 간단예제

package swt.registry;

import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

public class RegistryUserTest {
	// 주어진 노드에 키를 포함하고 있는지 체크한다.
	public static boolean contains(Preferences node, String key) {
		return node.get(key, null) != null;
	}

	public static void main(String args[]) throws BackingStoreException {
		
		Preferences userRootPrefs = Preferences.userRoot();
		//레지스트리에 시스템 Root못건드림 Preferences systemRootPrefs = Preferences.systemRoot();
		
		String key = "xname";
		if (contains(userRootPrefs, key)) {
			String value = userRootPrefs.get(key, "");	//있으면 있는거 출력
			System.out.println("xname : " + value);
		} else {
			String value = "upark";
			userRootPrefs.put(key, value);	//없으면 만들어 set
			System.out.println("create uname : " + value);
		}
	}
}

HKEY_CLASSES_ROOT레지스트리에 http\\shell\\open\\command  에 보면 default 브라우저 설정

package swt.registry;

import java.util.Enumeration;

import com.ice.jni.registry.NoSuchValueException;
import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;

public class JNIRegistryTest {
	private static final String REG_SWT_PARAMS = "Software\\RnDClub\\Client\\Params";

	public static final String REG_AMS_KEY_CLASSNAME = "AMS";

	public static final String REG_AMS_KEY_ID = "id";

	public static final String REG_AMS_KEY_DIR_HOME = "home";

	public static final String REG_AMS_KEY_DB_DRIVER = "dbDriver";

	public static final String REG_AMS_KEY_DB_URL = "dbUrl";

	public static final String REG_AMS_KEY_DB_USER = "dbUser";

	public static final String REG_AMS_KEY_DB_PASS = "dbPass";

	private static RegistryKey amsClientParamsRkey = null;

	public static String getDefaultBrowserCommand() {
		String value = null;
		String name = "http\\shell\\open\\command";
		try {
			RegistryKey rkey = Registry.HKEY_CLASSES_ROOT.createSubKey(name,
					"", RegistryKey.ACCESS_ALL);
			// System.out.println("rkey : " + rkey);
			value = rkey.getStringValue("");
		} catch (RegistryException e) {
			e.printStackTrace();
		}
		return (value);
	}

	public static RegistryKey getCurrentUserRegistryKey(String name) {
		RegistryKey rkey = null;
		try {
			rkey = Registry.HKEY_CURRENT_USER.createSubKey(name,
					"", RegistryKey.ACCESS_ALL);
			if (rkey.wasCreated() == true) {
				System.out.println("'" + name + "' rkey was created...");
			}
		} catch (RegistryException e) {
			e.printStackTrace();
		}
		return (rkey);
	}

	public static String getAmsClientParameter(String name) {
		String value = null;
		try {
			if (amsClientParamsRkey == null) {
				amsClientParamsRkey = getCurrentUserRegistryKey(REG_SWT_PARAMS);
			}
			value = amsClientParamsRkey.getStringValue(name);
		} catch (RegistryException e) {
			e.printStackTrace();
		}
		return (value);
	}

	public static void putAmsClientParameter(String name, String str) {
		try {
			if (amsClientParamsRkey == null) {
				amsClientParamsRkey = getCurrentUserRegistryKey(REG_SWT_PARAMS);
			}
			RegStringValue value = new RegStringValue(amsClientParamsRkey,
					name, str);
			amsClientParamsRkey.setValue(name, value);
		} catch (NoSuchValueException e) {
			e.printStackTrace();
		} catch (RegistryException e) {
			e.printStackTrace();
		}
	}

	public static void test() {
		try {
			if (amsClientParamsRkey == null) {
				amsClientParamsRkey = getCurrentUserRegistryKey(REG_SWT_PARAMS);
			}
			System.out.println("getFullName : "
					+ amsClientParamsRkey.getFullName());
			System.out.println("getMaxSubkeyLength : "
					+ amsClientParamsRkey.getMaxSubkeyLength());
			System.out.println("getMaxValueDataLength : "
					+ amsClientParamsRkey.getMaxValueDataLength());
			System.out.println("getMaxValueNameLength : "
					+ amsClientParamsRkey.getMaxValueNameLength());
			System.out.println("getName : " + amsClientParamsRkey.getName());
			System.out.println("getNumberSubkeys : "
					+ amsClientParamsRkey.getNumberSubkeys());
			System.out.println("getNumberValues : "
					+ amsClientParamsRkey.getNumberValues());

			Enumeration e = amsClientParamsRkey.valueElements();
			while (e.hasMoreElements()) {
				String key = (String) e.nextElement();
				String val = amsClientParamsRkey.getStringValue(key);
				System.out.println("key : " + key + ", val : " + val);
			}
			/*
			 * System.out.println(); for (int i = 0; i <
			 * amsClientParamsRkey.getNumberValues(); i++) { String key =
			 * amsClientParamsRkey.regEnumValue(i); String val =
			 * amsClientParamsRkey.getStringValue(key); System.out.println("[" +
			 * i + "] key : " + key + ", val : " + val); }
			 */
		} catch (RegistryException e) {
			e.printStackTrace();
		}
	}

	public static void main(String args[]) {
		test();

		putAmsClientParameter("server", "127.0.0.1");
		String value = getAmsClientParameter("server");
		System.out.println("server : " + value);

		putAmsClientParameter("dbUrl",
				"jdbc:oracle:thin:@127.0.0.1:1521:opendb");
		value = getAmsClientParameter("dbUrl");
		System.out.println("dbUrl : " + value);

		putAmsClientParameter("workspace", "D:\\workspace");
		value = getAmsClientParameter("workspace");
		System.out.println("workspace : " + value);

		putAmsClientParameter("home", "D:\\Temp\\ams_client");
		value = getAmsClientParameter("home");
		System.out.println("home : " + value);

		putAmsClientParameter("id", "ywoopark");
		value = getAmsClientParameter("id");
		System.out.println("id : " + value);

		value = getDefaultBrowserCommand();
		System.out.println("ie : " + value);
	}
}

위 경우는 현재 32bit만 가능하며 32bit인경우 registry.jar,ICE_JNIRegistry.dll 가 필요하며

registry.jar에 Native에 ICE_JNIRegistry.dll를 추가 설정해주어야 가능하다.