C# 32位程序在64位系统下注册表操作

时间:2021-09-07 01:28:51

  在64位的Windows操作系统中,为了兼容32位程序的运行,64位的Windows操作系统采用重定向机制。目的是为了能让32位程序在64位的操作系统不仅能操作关键文件文夹和关键的注册表并且又要避免与64位程序冲突

相关资料请查看32位程序在64位系统下运行的重定向机制

下面是以获取操作系统安装密匙KEY的案例:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Win32; namespace ReadProductKey { class Program { static void Main(string[] args) { string BackupProductKeyDefault; RegistryKey localKey = null; try {
          //判断操作系统版本(64位\32位)打开注册表项 localKey
= RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem?RegistryView.Registry64: RegistryView.Registry32);
          //得到注册表键值 BackupProductKeyDefault
= localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform").GetValue("BackupProductKeyDefault", "").ToString(); Console.WriteLine(BackupProductKeyDefault); Console.ReadLine(); } catch (Exception ex) { } } } }

程序执行界面:

C# 32位程序在64位系统下注册表操作

注意:如果以 RegistryView.Registry32打开注册表项,,并编译为32位版本的程序是不能获取系统关键注册表键值的