C# 32位应用读写64位系统的注册表

_is64Bit = Environment.Is64BitOperatingSystem;

		private object ReadRegistryKeyValue(string keyPath, string valueName)
        {
            object obj = null;
            using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                _is64Bit ? RegistryView.Registry64 : RegistryView.Default)) // HKEY_LOCAL_MACHINE\
			{
                using (var pathKey = baseKey.OpenSubKey(keyPath)) // e.g:SOFTWARE\Microsoft\DirectX
				{
                    obj= pathKey?.GetValue(valueName, null);
				}
            }
            return obj;
            //return Registry.GetValue(keyPath, valueName, null);
        }

		private void WriteRegistryKeyValue(string keyPath, string valueName, object value)
		{
            using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                _is64Bit ? RegistryView.Registry64 : RegistryView.Default)) // HKEY_LOCAL_MACHINE\
			{
                using (var pathKey = baseKey.OpenSubKey(keyPath)) // e.g:SOFTWARE\Microsoft\DirectX
				{
					if (pathKey != null)
                    {
                        pathKey.SetValue(valueName, value);
                    }
				}
            }
			//Registry.SetValue(keyPath, valueName, value);
		}

posted @ 2021-05-14 12:59  devs  阅读(156)  评论(0编辑  收藏  举报