转自:读取64位注册表 http://blog.sina.com.cn/s/blog_5f8817250100voon.html
C# 判断windows版本 位数 http://blog.csdn.net/zfd_007/article/details/5881269
参考:http://www.codeproject.com/Articles/51326/Net-Compilation-registry-accessing-and-applicatio
1. 读取64位操作系统注册表
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegOpenKeyEx")]
static extern int RegOpenKeyEx(IntPtr hKey, string subKey, uint options, int sam,
out IntPtr phkResult);
[Flags]
public enum eRegWow64Options : int
{
None = 0x0000,
KEY_WOW64_64KEY = 0x0100,
KEY_WOW64_32KEY = 0x0200,
// Add here any others needed, from the table of the previous chapter
}
[Flags]
public enum eRegistryRights : int
{
ReadKey = 131097,
WriteKey = 131078,
}
public static RegistryKey OpenSubKey(RegistryKey pParentKey, string pSubKeyName,
bool pWriteable,
eRegWow64Options pOptions)
{
if (pParentKey == null || GetRegistryKeyHandle(pParentKey).Equals(System.IntPtr.Zero))
throw new System.Exception("OpenSubKey: Parent key is not open");
eRegistryRights Rights = eRegistryRights.ReadKey;
if (pWriteable)
Rights = eRegistryRights.WriteKey;
System.IntPtr SubKeyHandle;
System.Int32 Result = RegOpenKeyEx(GetRegistryKeyHandle(pParentKey), pSubKeyName, 0,
(int)Rights | (int)pOptions, out SubKeyHandle);
if (Result != 0)
{
System.ComponentModel.Win32Exception W32ex =
new System.ComponentModel.Win32Exception();
throw new System.Exception("OpenSubKey: Exception encountered opening key",
W32ex);
}
return PointerToRegistryKey(SubKeyHandle, pWriteable, false);
}
private static System.IntPtr GetRegistryKeyHandle(RegistryKey pRegisteryKey)
{
Type Type = Type.GetType("Microsoft.Win32.RegistryKey");
FieldInfo Info = Type.GetField("hkey", BindingFlags.NonPublic | BindingFlags.Instance);
SafeHandle Handle = (SafeHandle)Info.GetValue(pRegisteryKey);
IntPtr RealHandle = Handle.DangerousGetHandle();
return Handle.DangerousGetHandle();
}
private static RegistryKey PointerToRegistryKey(IntPtr hKey, bool pWritable,
bool pOwnsHandle)
{
// Create a SafeHandles.SafeRegistryHandle from this pointer - this is a private class
BindingFlags privateConstructors = BindingFlags.Instance | BindingFlags.NonPublic;
Type safeRegistryHandleType = typeof(
SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType(
"Microsoft.Win32.SafeHandles.SafeRegistryHandle");
Type[] safeRegistryHandleConstructorTypes = new Type[] { typeof(System.IntPtr),
typeof(System.Boolean) };
ConstructorInfo safeRegistryHandleConstructor =
safeRegistryHandleType.GetConstructor(privateConstructors,
null, safeRegistryHandleConstructorTypes, null);
Object safeHandle = safeRegistryHandleConstructor.Invoke(new Object[] { hKey,
pOwnsHandle });
// Create a new Registry key using the private constructor using the
// safeHandle - this should then behave like
// a .NET natively opened handle and disposed of correctly
Type registryKeyType = typeof(Microsoft.Win32.RegistryKey);
Type[] registryKeyConstructorTypes = new Type[] { safeRegistryHandleType,
typeof(Boolean) };
ConstructorInfo registryKeyConstructor =
registryKeyType.GetConstructor(privateConstructors, null,
registryKeyConstructorTypes, null);
RegistryKey result = (RegistryKey)registryKeyConstructor.Invoke(new Object[] {
safeHandle, pWritable });
return result;
}
How to use the Code
The OpenSubKey will return the searched key, allowing you to specify reading from the normal registry, or from the alternative 32-bit, WOW64 registry. The following example reads from the 32-bit WOW64 registry:
try
{
RegistryKey key = OpenSubKey(Registry.LocalMachine,"Software\\[Key]",false,
eRegWow64Options.KEY_WOW64_32KEY);
}
catch
{
// Parent key not open, exception found at opening (probably related to
// security permissions requested)
}
2.判断操作系统 位数
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
internal short wProcessorArchitecture;
internal short wReserved;
internal int dwPageSize;
internal IntPtr lpMinimumApplicationAddress;
internal IntPtr lpMaximumApplicationAddress;
internal IntPtr dwActiveProcessorMask;
internal int dwNumberOfProcessors;
internal int dwProcessorType;
internal int dwAllocationGranularity;
internal short wProcessorLevel;
internal short wProcessorRevision;
}
/// <summary>
/// 操作系统操作类(获取系统版本、位数)
/// </summary>
public class WindowsHelper
{
/// <summary>
/// Windows系统版本
/// </summary>
public enum WindowsVersion { Unknown, Win95, Win98, WinME, NT351, NT40, Win2K, XP, Win2003, Vista, Win7 };
/// <summary>
/// Windows系统操作系统位数
/// </summary>
public enum WindowsBits { Unknown, OS16Bits, OS32Bits, OS64Bits };
private const short PROCESSOR_ARCHITECTURE_INTEL = 0;
private const short PROCESSOR_ARCHITECTURE_IA64 = 6;
private const short PROCESSOR_ARCHITECTURE_AMD64 = 9;
private const int PROCESSOR_ARCHITECTURE_UNKNOWN = 0x00FFFF;
[DllImport("kernel32.dll")]
static extern void GetNativeSystemInfo(out SYSTEM_INFO lpSystemInfo);
private static bool IsPlatformX64()
{
SYSTEM_INFO sysinfo = new SYSTEM_INFO();
bool s_OK = false;
GetNativeSystemInfo(out sysinfo);
switch (sysinfo.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_IA64:
s_OK = true;
break;
case PROCESSOR_ARCHITECTURE_AMD64:
s_OK = true;
break;
default:
s_OK = false;
break;
}
return s_OK;
}
/// <summary>
/// 获取操作系统的版本
/// </summary>
/// <returns></returns>
public static WindowsVersion GetWindowsVersion()
{
WindowsVersion version = WindowsVersion.XP;
OperatingSystem osInfo = Environment.OSVersion;
switch (osInfo.Platform)
{
case System.PlatformID.Win32Windows:
switch (osInfo.Version.Minor)
{
case 0:
version = WindowsVersion.Win95;
break;
case 10:
version = WindowsVersion.Win98;
break;
case 90:
version = WindowsVersion.WinME;
break;
}
break;
case System.PlatformID.Win32NT:
switch (osInfo.Version.Major)
{
case 3:
version = WindowsVersion.NT351;
break;
case 4:
version = WindowsVersion.NT40;
break;
case 5:
switch (osInfo.Version.Minor)
{
case 0:
version = WindowsVersion.Win2K;
break;
case 1:
version = WindowsVersion.XP;
break;
default:
version = WindowsVersion.Win2003;
break;
}
break;
case 6:
switch (osInfo.Version.Minor)
{
case 0:
version = WindowsVersion.Vista;
break;
default:
version = WindowsVersion.Win7;
break;
}
break;
default:
version = WindowsVersion.Win7;
break;
}
break;
}
return version;
}
/// <summary>
/// 获取操作系统的位数
/// </summary>
/// <returns></returns>
public static WindowsBits GetWindowsBits()
{
WindowsBits OSBits;
if (IsPlatformX64())
{
OSBits = WindowsBits.OS64Bits;
}
else
{
OSBits = WindowsBits.OS32Bits;
}
return OSBits;
}
}