public static string GetRegistryValue(string path, string key) { RegistryKey regkey = null; try { regkey = Registry.LocalMachine.OpenSubKey(path); if (regkey == null) { Log2.Log("Cannot find Registry path:" + path); return(null); } object val = regkey.GetValue(key); if (val == null) { Log2.Log("Cannot find Registry key:" + key); return(null); } return(val.ToString()); } catch (Exception ex) { Log2.Log(ex.ToString()); return(null); } finally { if (regkey != null) { regkey.Close(); } } }
/// <summary> /// 设置64位注册表 /// </summary> /// <param name="key"></param> /// <param name="subKey"></param> /// <param name="name"></param> /// <param name="value"></param> /// <returns></returns> public static int Set64BitRegistryKey(string key, string subKey, string name, string value) { int STANDARD_RIGHTS_ALL = (0x001F0000); int KEY_QUERY_VALUE = (0x0001); int KEY_SET_VALUE = (0x0002); int KEY_CREATE_SUB_KEY = (0x0004); int KEY_ENUMERATE_SUB_KEYS = (0x0008); int KEY_NOTIFY = (0x0010); int KEY_CREATE_LINK = (0x0020); int SYNCHRONIZE = (0x00100000); int KEY_WOW64_64KEY = (0x0100); int REG_OPTION_NON_VOLATILE = (0x00000000); int KEY_ALL_ACCESS = (STANDARD_RIGHTS_ALL | KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY | KEY_CREATE_LINK) & (~SYNCHRONIZE); int ret = 0; try { //将Windows注册表主键名转化成为不带正负号的整形句柄(与平台是32或者64位有关) IntPtr hKey = TransferKeyName(key); //声明将要获取Key值的句柄 IntPtr pHKey = IntPtr.Zero; //获得操作Key值的句柄 int lpdwDisposition = 0; ret = RegCreateKeyEx(hKey, subKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS | KEY_WOW64_64KEY, IntPtr.Zero, out pHKey, out lpdwDisposition); if (ret != 0) { Log2.Log(string.Format("Unable to create key {0} - {1}: {2}!", key, subKey, ret)); return(ret); } //关闭注册表转向(禁止特定项的注册表反射) RegDisableReflectionKey(pHKey); //设置访问的Key值 uint REG_SZ = 1; byte[] data = Encoding.Unicode.GetBytes(value); RegSetValueEx(pHKey, name, 0, REG_SZ, data, (uint)data.Length); //打开注册表转向(开启特定项的注册表反射) RegEnableReflectionKey(pHKey); RegCloseKey(pHKey); } catch (Exception ex) { Log2.Log(ex.ToString()); return(-1); } return(ret); }