标签:style class blog code java http
今天在工作的过程中遇到个奇怪的为问题,就是通过c#获取注册表键值的问题,一般都比较简单:
string SQLPath = string.Empty; RegistryKey hkml = Registry.LocalMachine; RegistryKey MSSQLServerKey = hkml.OpenSubKey(@"SOFTWARE\MICROSOFT\MSSQLServer"); if (MSSQLServerKey != null) { string[] keys = MSSQLServerKey.GetSubKeyNames(); RegistryKey setupKey = MSSQLServerKey.OpenSubKey("Setup"); if(setupKey!=null) { SQLPath = setupKey.GetValue("SQLPath").ToString(); } }
但是我要取得值就是没有得到,debug,发现setupkey返回为null,打开本机的注册表是有的阿,奇了怪了……
这是怎么回事???? 问了同事,他也觉得奇怪。 后来我发现是不是注册表位数问题啊,一下子找到为什么系统返回是缺少的那些键值。
发现Wow6432Node下面返回的就是我们在debug时候的返回值。
这下感觉找到了解决问题的方向了。就是要解决32位程序访问64位系统上的Windows注册表的问题。
最后在网上找到了两位高人的解法,我在这里都贴出来
使用.netFx4.0提供的方法解决32位程序访问64位系统的64位注册表
最后我尝试使用了后一种方法,解决了这个问题。在此,非常感谢浪谷的解决方案。
public static string GetSQLServerInstallPath() { string SQLPath = string.Empty; RegistryKey hkml = Registry.LocalMachine; RegistryKey MSSQLServerKey = hkml.OpenSubKey(@"SOFTWARE\MICROSOFT\MSSQLServer"); if (MSSQLServerKey != null) { string[] keys = MSSQLServerKey.GetSubKeyNames(); RegistryKey setupKey = MSSQLServerKey.OpenSubKey("Setup"); if(setupKey!=null) { SQLPath = setupKey.GetValue("SQLPath").ToString(); } else { SQLPath = Get64BitRegistryKey(RegistryHive.LocalMachine, @"SOFTWARE\MICROSOFT\MSSQLServer\Setup", "SQLPath", RegistryView.Registry64); } } return SQLPath; } /// <summary> /// this method is use for the 32appliction to get the 64bit regestkey /// </summary> /// <param name="parentKeyName"></param> /// <param name="subKeyName"></param> /// <param name="keyName"></param> /// <returns></returns> public static string Get64BitRegistryKey(RegistryHive hive,string keyName,string valueName,RegistryView view) { Microsoft.Win32.SafeHandles.SafeRegistryHandle handle = new Microsoft.Win32.SafeHandles.SafeRegistryHandle(GetHiveHandle(hive),true); RegistryKey subkey = RegistryKey.FromHandle(handle, view).OpenSubKey(keyName); RegistryKey key = RegistryKey.FromHandle(subkey.Handle, view); return key.GetValue(valueName).ToString(); } static IntPtr GetHiveHandle(RegistryHive hive) { IntPtr preexistingHandle = IntPtr.Zero; IntPtr HKEY_CLASSES_ROOT = new IntPtr(-2147483648); IntPtr HKEY_CURRENT_USER = new IntPtr(-2147483647); IntPtr HKEY_LOCAL_MACHINE = new IntPtr(-2147483646); IntPtr HKEY_USERS = new IntPtr(-2147483645); IntPtr HKEY_PERFORMANCE_DATA = new IntPtr(-2147483644); IntPtr HKEY_CURRENT_CONFIG = new IntPtr(-2147483643); IntPtr HKEY_DYN_DATA = new IntPtr(-2147483642); switch (hive) { case RegistryHive.ClassesRoot: preexistingHandle = HKEY_CLASSES_ROOT; break; case RegistryHive.CurrentUser: preexistingHandle = HKEY_CURRENT_USER; break; case RegistryHive.LocalMachine: preexistingHandle = HKEY_LOCAL_MACHINE; break; case RegistryHive.Users: preexistingHandle = HKEY_USERS; break; case RegistryHive.PerformanceData: preexistingHandle = HKEY_PERFORMANCE_DATA; break; case RegistryHive.CurrentConfig: preexistingHandle = HKEY_CURRENT_CONFIG; break; case RegistryHive.DynData: preexistingHandle = HKEY_DYN_DATA; break; } return preexistingHandle; }
32位程序访问64位系统上的Windows注册表,布布扣,bubuko.com
标签:style class blog code java http
原文地址:http://www.cnblogs.com/geixinyigejia/p/3782322.html