在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) { } } } }
程序執行界面:
注意:如果以 RegistryView.Registry32打開注冊表項,並編譯為32位版本的程序是不能獲取系統關鍵注冊表鍵值的