如何判断我的应用程序(在 Visual Studio 2008 中编译为Any CPU)是作为 32 位还是 64 位应用程序运行的?
Lawrence Johnston
问问题
27514 次
5 回答
160
于 2010-08-11T18:25:33.047 回答
71
if (IntPtr.Size == 8)
{
// 64 bit machine
}
else if (IntPtr.Size == 4)
{
// 32 bit machine
}
于 2008-12-29T13:09:51.077 回答
5
我从Martijn Boven那里找到了这段代码,它可以解决问题:
public static bool Is64BitMode() {
return System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8;
}
于 2008-11-05T18:09:05.593 回答
5
Microsoft All-In-One Code Framework 中的此代码示例可以回答您的问题:
在C#中检测进程运行平台(CSPlatformDetector)
CSPlatformDetector 代码示例演示了与平台检测相关的以下任务:
- 检测当前操作系统的名称。 (例如“Microsoft Windows 7 Enterprise”)
- 检测当前操作系统的版本。 (例如“Microsoft Windows NT 6.1.7600.0”)
- 判断当前操作系统是否为 64 位操作系统。
- 判断当前进程是否为64位进程。
- 确定系统上运行的任意进程是否为 64 位。
如果只想确定当前运行的进程是否为 64 位进程,可以使用 .NET Framework 4 中新增的Environment.Is64BitProcess属性。
而如果要检测系统上运行的任意应用程序是否为64位进程,则需要确定OS位数,如果是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;
}
于 2011-09-04T02:47:40.857 回答
0
在 .Net Standard 中,您可以使用System.Runtime.InteropServices.RuntimeInformation.OSArchitecture
于 2017-08-10T15:00:29.253 回答