32位程式訪問64位系統上的Windows註冊表

來源:互聯網
上載者:User

標籤: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註冊表的問題。

最後在網上找到了兩位高人的解法,我在這裡都貼出來

C# 32位程式訪問64位系統註冊表

使用.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;        }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.