2008-11-05 77 views

回答

61
if (IntPtr.Size == 8) 
{ 
    // 64 bit machine 
} 
else if (IntPtr.Size == 4) 
{ 
    // 32 bit machine 
} 
+0

编译器不在中间扮演什么角色? – 2014-04-22 02:04:16

5

我发现这个代码Martijn Boven该做的伎俩:

public static bool Is64BitMode() { 
    return System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8; 
} 
+23

很可能更有效的调用IntPtr.Size代替Marshal.SizeOf(typeof运算(IntPtr的)) – JaredPar 2008-11-05 19:20:22

+2

我很乐意给+1到JaredPar的意见;我就是这么做的...... – 2008-11-05 20:32:02

131

如果您使用.NET 4.0,这是一个班轮当前进程:

Environment.Is64BitProcess 

参考:Environment.Is64BitProcess Property(MSDN)

+1

感谢您发布答案,这很了解。我不打算改变当前接受的答案,因为这个问题最初是关于.NET 3.5的,但我会鼓励人们为你的答案投票。 – 2010-08-11 20:26:30

5

从微软此代码示例多功能一站式示例代码库可以回答你的问题:

Detect the process running platform in C# (CSPlatformDetector)

的CSPlatformDetector代码示例演示了以下任务 与平台检测:

  1. 检测当前操作系统的名称。 (例如“Microsoft Windows 7 Enterprise”)
  2. 检测当前操作系统的版本。 (例如“Microsoft Windows NT 6.1.7600.0”)
  3. 确定当前操作系统是否是64位操作系统。
  4. 确定当前进程是否是64位进程。
  5. 确定在系统上运行的任意进程是否为64位。

如果你只是想确定是否当前运行的进程是一个64位 过程中,可以使用Environment.Is64BitProcess属性,它是在.NET框架 4.

如果你新要检测的系统 上运行任意应用是否是一个64位的过程中,你需要确定操作系统位数,如果是64位的, 呼叫IsWow64Process()与目标进程句柄:

static bool Is64BitProcess(IntPtr hProcess) 
{ 
    bool flag = false; 

    if (Environment.Is64BitOperatingSystem) 
    { 
     // On 64-bit OS, if a process is not running under Wow64 mode, 
     // the process must be a 64-bit process. 
     flag = !(NativeMethods.IsWow64Process(hProcess, out flag) && flag); 
    } 

    return flag; 
} 
0

在.net标准,你可以使用System.Runtime.InteropServices.RuntimeInformation.OSArchitecture

相关问题